我的Angular代码出了什么问题?我得到以下错误:

无法读取BrowserDomAdapter.removeClass中未定义的属性“remove”

<ol>
  <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>

当前回答

这对我来说很管用:

[ngClass]="{'active': dashboardComponent.selected_menu == 'profile'}"

其他回答

在下面的例子中,你可以使用IF ELSE

<p class="{{condition ? 'checkedClass' : 'uncheckedClass'}}">
<p [ngClass]="condition ? 'checkedClass' : 'uncheckedClass'">
<p [ngClass]="[condition ? 'checkedClass' : 'uncheckedClass']">

你应该像这样使用一些东西([ngClass]而不是*ngClass):

<ol class="breadcrumb">
  <li [ngClass]="{active: step==='step1'}" (click)="step='step1; '">Step1</li>
  (...)

<div class="collapse in " [ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''" id="collapseExampleOrganization" aria-expanded="true" style=""> <ul> <li class="nav-item" [ngClass]="{'active': active_tab=='manage'}"> <a routerLink="/main/organization/manage" (click)="activemenu('manage')"> <i class="la la-building-o"></i> <p>Manage</p></a></li> <li class="nav-item" [ngClass]="{'active': active_tab=='assignservice'}"><a routerLink="/main/organization/assignservice" (click)="activemenu('assignservice')"><i class="la la-user"></i><p>Add organization</p></a></li> </ul></div>

代码是ngClass if else条件的好例子。

[ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''"

[ngClass]="{'active': active_tab=='assignservice'}"

可以使用以下语法使类成为动态的。在Angular 2 plus中,你可以通过多种方式做到这一点:

[ngClass]="{'active': arrayData.length && arrayData[0]?.booleanProperty}"
[ngClass]="{'active': step}"
[ngClass]="step== 'step1'?'active':''"
[ngClass]="step? 'active' : ''"

让,YourCondition是你的条件或布尔属性,然后像这样做

[class.yourClass]="YourCondition"