我正在尝试更改基于数组的某些元素的类。
我已经声明了一个函数来返回一个 bool 值并说明字符串(状态/类)是否包含在数组中。
我将其命名为 isState(st: string) {return (this.ArrayWithClasses.indexOf(st) > 0)}
然后我做
[ngClass]="{'class-I-Want-To-Activate': isState('evaluating-this-state') }"
但它不起作用。你看到我的错误了吗?最佳解决方案?
编辑:如果我只使用一个 bool 值来切换类,它就可以工作。所以我考虑是不是函数出了什么问题...
最佳答案
您不应该检查 indexOf()
值是否为 > 0
,因为如果该字符串位于数组的第一个位置,您将得到 0。
isState(st: string) {
let temp = this.ArrayWithClasses.indexOf(st);
if(temp != -1)
return true;
else
return false;
}
如果搜索到的字符串不在数组中,您将得到 -1
值。
关于javascript - Angular 2 ng类取决于功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43297324/