在Angular中,根据条件向HTML元素添加额外的类
在Angular中,根据条件向HTML元素添加额外的类
我的Angular代码出了什么问题?我得到以下错误:
无法读取未定义的属性\'remove\',位于BrowserDomAdapter.removeClass
-
- Step1
-
- Step2
-
- Step3
admin 更改状态以发布 2023年5月22日
[ngClass]=...
而不是*ngClass
。
*
仅用于结构指令的速记语法,例如可以使用
{{item}}
而不是更长的等效版本
{{item}}
另请参阅https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html
... ... ... ... ...
另请参阅https://angular.io/docs/ts/latest/guide/template-syntax.html
The class binding is specialThis one is not so special
Bad curly
Angular 2+提供了多种条件添加类的方式:
第一种方式
[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'"
你可以在文档页面上找到这些示例。