作者:夕阳何处寻 | 来源:互联网 | 2023-01-29 16:52
从教程中,讲师提出了这个:
零件:
isFavorite = false;
toggleClass() {
this.isFavorite != this.isFavorite;
}
我的方式是:
零件:
classes = "glyphicon-star-empty";
toggleClasses() {
this.classes == "glyphicon-star-empty"? this.classes = "glyphicon-star" : this.classes = "glyphicon-star-empty";
}
两种方式都按预期工作,但我觉得从性能角度来看,我的方式并不是很好,因为我正在使用循环,对吧?
对不起,如果答案可能只是"是".我只是想确定它是如何工作的(特别是对所有那些Ng指令感兴趣,比如ngModel,ngClass).
谢谢
1> umar..:
最好的方法就是这样写.
[ngClass]="{'class1': isFavorite, 'class2': !isFavorite}"
喜欢
然后你可以绑定toggleHandler
toggleClass() {
this.isFavorite != this.isFavorite;
}