What is wrong with my Angular code? I am getting:
Cannot read property 'remove' of undefined at BrowserDomAdapter.removeClass ...
HTML
<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>
</ol>
Angular version 2,...,9 provides several ways to add classes conditionally:
type one
[class.my-class]="step=='step1'"
type two
[ngClass]="{'my-class': step=='step1'}"
and multiple option:
[ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }"
type three
[ngClass]="{1:'my-class1',2:'my-class2',3:'my-class4'}[step]"
type four
[ngClass]="(step=='step1')?'my-class1':'my-class2'"
Perfect answer, just fix the type 2 to: [ngClass]="{'my-class': step=='step1'}" With the '' int the class name
For type three, the order of the class name and check is wrong. It should be class name first such as [ngClass]="{ 'my-class1': 1, 'my-class2': 2 }"
looks like "type three" and "type four" are specific usages of
[ngClass]="js expression returning html class string"
so that are the same in this senseThanks man.. you are awesome
Perfect answer mate. Thanks so much !