I'm working on my post-graduate assignment where I have to create a simple calculator. I'm almost there but the final element - making the negative result red and positive green is still missing. The result must be seen in an input field
I was trying to use [ngClass]= with two conditions but it is not working:
<input type="number" [ngStyle]="resultStyling" value="{{ result }}" name="resultInput"
[ngClass]="{
'red': <=0,
'green': !<=0
}">
the public classes in component.ts for the calculator are:
public field1: number;
public field2: number;
public result: number;
public isPositive: boolean = true;
The HTML:
<input type="number" [ngStyle]="inputStyling" name="field1" [(ngModel)]="field1"><br>
<button (click)="add()" [ngStyle]="buttonStyling">+</button>
<button (click)="substract()" [ngStyle]="buttonStyling">-</button>
<button (click)="multiply()" [ngStyle]="buttonStyling" >*</button>
<button (click)="divide()" [ngStyle]="buttonStyling">/</button><br><input type="number" [ngStyle]="inputStyling" name="field2" [(ngModel)]="field2">
<br>=<br>
<input type="number" [ngStyle]="resultStyling" value="{{ result }}" name="resultInput">
And the CSS are just 2 simple classes `.red {color: red} and .green {color: green}.
I can't figure out how to make the result show in color. Do you have any suggestions what may be the solution? Maybe the [ngClass] should just be written in a different way?
The whole code may be also found on my stackblitz: https://stackblitz.com/edit/k-rapacz-angular-day1-z6vxu1
Thank you in advance for the support!
'red': <=0looks suspicious. How is it supposed to know what should be tested against0?'green': !<=0looks even more suspicious, you might consider>. Finally, both of those conditions are trying to apply to 0. One of them (probably red) shouldn't include 0 (e.g.,<0for red and>=0for green).