Angular: conditional class with *ngClass

daniel picture daniel · Feb 8, 2016 · Viewed 865.5k times · Source

What is wrong with my Angular code? I am getting:

Cannot read property 'remove' of undefined at BrowserDomAdapter.removeClass ...


<ol class="breadcrumb">
    <li *ngClass="{active: step==='step1'}" (click)="step='step1; '">Step1</li>
    <li *ngClass="{active: step==='step2'}"  (click)="step='step2'">Step2</li>
    <li *ngClass="{active: step==='step3'}" (click)="step='step3'">Step3</li>


MostafaMashayekhi picture MostafaMashayekhi · Feb 1, 2017

Angular version 2,...,9 provides several ways to add classes conditionally:

type one

[]="step === 'step1'"

type two

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

and multiple option:

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

type three


type four