I want to use multiple classes inside Angular [ngClass]. I have two classes, It should work accordingly as per the condition of the flag, that are already passed from the component.ts.
5 Answers
You can do this in several ways :
number one :
[class.my-class]="step=='step1'"
number twe :
[ngClass]="{'my-class': step=='step1'}"
number three :
[ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }"
number four :
[ngClass]="(step=='step1')?'my-class1':'my-class2'"
you can get help this link for more help
Comments
You can do like this
[ngClass]="{'class1':condition1, 'class2':condition2}".
1 Comment
Jitendra
you can also add condition like
conditionMatch ? true : false.html :
<div [ngClass]="{'class1' : value == 1, 'class2' : value == 2}">
.......................
</div>
by using a function
<div [ngClass]="getClass(2)">
.......................
</div>
ts :
export class AppComponent {
value = 1;
getClass(value){
if(value == 1) return 'class1'
else if(value == 2) return 'class2'
}
}