Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - Angular:带* ngClass的条件类(Angular: conditional class with *ngClass)

What is wrong with my Angular code?

(我的Angular代码有什么问题?)

I am getting:

(我正进入(状态:)

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

HTML

(的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>
  ask by daniel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(Angular 2,..,7提供了几种有条件地添加类的方法:)

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'"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...