In Angular 1, the code below works well.
<div ng-class="$varA === $varB ? 'css-class-1' : 'css-class-2'">
But when I try to do similar thing in Angular 2. It does not work.
I already added directives: [NgClass]
<div [ngClass]="varA === varB ? 'css-class-1' : 'css-class-2'">
How should I write in Angular 2, thanks!
EDIT: It was my mistake, I accidentally added {
}
to the whole varA === varB ? 'css-class-1' : 'css-class-2'
. So ngClass still can use ternary operator in Angular 2.
Yes. What you wrote works:
<div [ngClass]="varA === varB ? 'css-class-1' : 'css-class-2'">
The result of the expression on the the right-hand side has to evaluate to one of the following:
Maybe you had some other error in your code?