I am new to Angular2
and was wondering how I go about setting a font color to an element depending on the value.
My scenario is: if the value of the input field is not 100 then I want it red but if it is 100 then I want it green.
I have the following code in place but cant get it working.
XXX.component.css
.red {
color: red;
}
.green {
color: green;
}
XXX.component.css
<input mdInput placeholder="Proportion '%'" [(ngModel)]="proportion ">
<p>hello <span ng-class='{red : proportion!= '100', green: proportion === '100'}'>{{proportion}}</span></p>
You can use it like this:
You can also bind the style property.
Since you use
Angular2
, So you need to use[ngClass]
, and your input model is bind toproportion
, So use it to compare,Do it like :
You need to modify your logic to have double quotes and ngModel proportion value
Hope it helps!!
There are two solutions to change font color but depends on you requirement
NgStyle
Directive which Update an HTML element styles for you..NgStyle directive Ex:
NgClass
Directive which Adds and removes CSS classes on an HTML element...NgClass directive Ex: