我的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]指令不相关,但我也得到了相同的错误

无法读取未定义的属性“移除”

和我认为是错误在我的[ngClass]条件,但它原来是我试图访问的[ngClass]条件的属性没有初始化。

就像我的打字文件里有这个

element: {type: string};

和在我的[ngClass]我正在使用

[ngClass]="{'active', element.type === 'active'}"

我得到了错误

无法读取未定义的属性“类型”

解决办法就是把我的财产

element: {type: string} = {type: 'active'};

希望它能帮助那些试图匹配[ngClass]中属性条件的人

其他回答

该指令有三种不同的操作方式,这取决于表达式的计算结果是三种类型中的哪一种:

If the expression evaluates to a string, the string should be one or more space-delimited class names. If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name. If the expression evaluates to an array, each element of the array should either be a string as in type 1 or an object as in type 2. This means that you can mix strings and objects together in an array to give you more control over what CSS classes appear. See the code below for an example of this.

    [class.class_one] = "step === 'step1'"

    [ngClass]="{'class_one': step === 'step1'}"

对于多个选项:

    [ngClass]="{'class_one': step === 'step1', 'class_two' : step === 'step2' }" 

    [ngClass]="{1 : 'class_one', 2 : 'class_two', 3 : 'class_three'}[step]"

    [ngClass]="step == 'step1' ? 'class_one' : 'class_two'"

[ngClass] =…而不是*ngClass。

*仅用于结构型指令的简写语法,例如可以使用

<div *ngFor="let item of items">{{item}}</div>

而不是较长的同等版本

<template ngFor let-item [ngForOf]="items">
  <div>{{item}}</div>
</template>

参见https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html

<some-element [ngClass]="'first second'"> <某个元素[ngClass] = "(“第一次”,“二”)" >…< /某个元素> <某个元素[ngClass] = "{“第一”:真的,“二”:真的,“第三”:假}" >…< /某个元素> <某个元素[ngClass] = " stringExp | arrayExp | objExp " >…< /某个元素> <some-element [ngClass]="{'class1 class2 class3': true}">

参见https://angular.io/docs/ts/latest/guide/template-syntax.html

<!——用属性>开启/关闭“特殊”类 < div[类。</div> . special]="isSpecial">类绑定是特殊的 <!——绑定到class。Special '胜过类属性——> < div class = "特殊的“ [class.special] = " !isSpecial">这个没有那么特别</div>

<!——用绑定重置/覆盖所有类名——> .使用实例 <div class="bad curly special" (类)= " badCurly " >坏花< / div >

Angular version 2+提供了几种有条件地添加类的方法:

1型

    [class.my_class] = "step === 'step1'"

两个类型

    [ngClass]="{'my_class': step === 'step1'}"

并有多种选择:

    [ngClass]="{'my_class': step === 'step1', 'my_class2' : step === 'step2' }"

三个类型

    [ngClass]="{1 : 'my_class1', 2 : 'my_class2', 3 : 'my_class4'}[step]"

四个类型

    [ngClass]="step == 'step1' ? 'my_class1' : 'my_class2'"

您可以在文档页上找到这些示例

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

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

对于elseif语句(较少比较),使用如下语句:(例如,比较三个语句)

<div [ngClass]="step === 'step1' ? 'class1' : (step === 'step2' ? 'class2' : 'class3')"> {{step}} </div>