Modify the dynamic style of an Angular input field

Looking for assistance with a text box

<input type="text" [ngStyle]="(show_div===true) ? {'border-color':'red','color':'red'} : {'border-color': 'green','color':'green'}" [(ngModel)]="values[i]"/> 

When the show_div variable is true, I want to style it with

{'border-color':'red','color':'red'}
, and if it is false, then with
{'border-color': 'green','color':'green'}
. However, initially, I would like the textbox and text color to be black. Even though green and red are working correctly, the text is set to green initially which is not desired.

If anyone has a solution, I would greatly appreciate it. Thank you!

Answer №1

To receive a more precise response, please provide the corresponding TS code. Based on the details given, it seems that the initial value of show_div is undefined. In this scenario, you should include an additional condition in your ternary statement like so:

<input  type="text" [ngStyle]="(show_div===true) ? {'border-color':'red','color':'red'} : show_div === undefined ? {'border-color': 'black','color':'black'} : {'border-color': 'green','color':'green'}" [(ngModel)]="values[i]"/> 

However, embedding these styles directly in your HTML can make it overly verbose and challenging to comprehend. Personally, I recommend defining these styles in your stylesheet and assigning them to a class. Then, dynamically switch between classes within your component instead of altering the styles.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prohibit using any as an argument in a function if a generic type is

I have attempted to implement this particular solution to prevent the calling of a generic function with the second type being equal to any. The following code snippet works fine as long as the first generic parameter is explicitly specified: declare fu ...

Guide to Generating a Library Using Angular-CLI

Can someone guide me on creating a custom Angular library using angular-cli and publishing it to the npm repository? I need to include other external libraries and expose my module along with some services in the index.ts file. Could you please provide s ...

The trouble with React Navigation encountered with TypeScript: This Entity Cannot Be Invoked

Currently, I'm facing a typescript issue after upgrading to React Navigation 5. The specific error message reads: There is an issue with calling this expression. The union type '{ <RouteName extends "Stack1Screen1" | "Home&quo ...

Tips for preventing repetition of code in multiple entry points in Rollup

My goal is to use rollup to process a group of input files and generate multiple output files in the dist directory that all have some common code shared between them. Below is my current rollup configuration: import path from 'path'; import pat ...

What sets React.HTMLProps<HTMLDivElement> apart from React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>?

Exploring the differences between interfaces and types in React: interface Properties1 extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {} interface Properties2 extends React.HTMLProps<HTMLDivElement> ...

Creating TypeScript declaration file for exporting JavaScript function

I'm a beginner with TypeScript and I want to learn how to create a declaration file for a custom JavaScript function. I attempted to do this, however, I encountered an error stating "Could not find a declaration file for module './main'." Ad ...

Sizing the Angular CDK overlay backdrop appropriately when connecting it to a component

Is there a way to have a CDK overlay specifically cover a certain component using flexibleConnectedTo()? I am struggling with ensuring that the overlay backdrop only covers the targeted component, rather than the entire page. The CSS for the cdk-overlay-co ...

Experiencing a "HEROES not found" error while following an Angular guide

I've been diving into Angular with the tutorial provided on https://angular.io. However, I've hit a roadblock at step 4. Displaying a list where I'm encountering an error in HeroesComponent. Cannot find name 'HEROES' The cod ...

Struggling to implement cookie functionality in Go language

I've been trying to figure out the issue by watching videos like this one and checking out this link. There's also a similar question on Stack Overflow This is the code I've been working with: func articlesHandler(w http.ResponseWriter, r * ...

A step-by-step guide on integrating Detox with jest (ts-jest) and Typescript in a react-native app

I've been experimenting with incorporating Typescript into my detox tests. The most relevant information I could find was in this gist. However, when trying to implement it, I encountered an error stating that jasmine is not defined. After researching ...

Import a Component Dynamically Using a Variable in AngularJS

I am attempting to dynamically load a component using a variable, but I keep running into an "Uncaught Error: Template parse errors". How can I achieve this successfully? <app-{{ this.plugin.component }}></app-{{ this.plugin.component }}> ...

Typescript encounters Duplicate error when method overloading is implemented

Below is the code snippet that I am working with: public async insert(data: iFlower | iFlower[]): Promise<iFlower> | Promise<iFlower[]> { await this.insert(data); } private async insert(data: iFlower): Promise<iFlower>{ .... return d ...

Style the date using moment

All languages had a question like this except for JavaScript. I am trying to determine, based on the variable "day," whether it represents today, tomorrow, or any other day. ...

Angular interceptor allows the execution of code after the outgoing request has completed its process

In the process of creating a simple interceptor, I have encountered an issue. The interceptor is designed to check if an outgoing request is targeting a specific endpoint type, namely events and favoriteevents. While the interceptor works almost as intend ...

Could it be possible for TypeScript inference to directly infer the value and omit the key in the process?

class A { state: B } class B { something: C } class C { a: string; b: boolean; } type MagicType = ... const c: MagicType<A> c.state.a = "123" c.state.b = true; Is it possible to achieve the mentioned functionality without altering the exi ...

Using AngularJS to inject a service into a static property of a standard class

For my current project, I am combining TypeScript and AngularJS. One of the challenges I'm facing is how to instantiate a static member of a public class (not controller, just a normal class) with a service object. When it comes to controllers, utiliz ...

Creating a specification for a type that lacks a specific concluding character

When utilizing TypeScript, you have the ability to create template literal types such as: type Paragraph = `${string}\n`; const paragraph: Paragraph = 'foo\n'; // valid const word: Paragraph = 'foo'; // invalid But can you d ...

Nebular specializes in crafting personalized registration solutions

I am currently working on a custom registration form that is different from the standard Nebular registration. Due to the custom form validation I have implemented, the standard registration system no longer functions as intended for me. I believe I need t ...

How can I handle the different data type returned by ReactDom.render?

My main focus is on rendering Markdown. Additionally, I also need to parse HTML which is passed as a string. In this scenario, children represents the HTML passed as a string, while isParseRequired indicates if parsing is needed. import cx from 'clas ...

Can NODE_PATH be configured in Typescript?

Before, I worked on my React app with ES6 and used NODE_PATH='src' to import files starting from the src folder. However, since switching to Typescript, I've realized that NODE_PATH is not supported. After some investigation, I discovered th ...