by @codeexamples

How to conditionally add classes in Angular using *ngClass

Home » angular » How to conditionally add classes in Angular using *ngClass

Angular offers us different ways to add classes conditionally to the html elements.

First way

[class.my-class]="step=='step1'"

Second way

[ngClass]="{'my-class': step=='step1'}"

Third way

[ngClass]="(step=='step1')?'my-class1':'my-class2'"

Adding multiple classes

[ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }"