简体   繁体   English

无法在Angular JS中使用条件ng-class?

[英]Trouble using conditional ng-class in Angular JS?

I have the following anchor tag: 我有以下锚标记:

<a href="#/home" ng-class="{ active: navCtrl.checkCurrent('home')" ng-click="navCtrl.setLink('home')" class="navigation-anchor">Home</a>

I have confirmed by manually adding active to the class attribute of this anchor that my class produces the intended result. 通过手动将active添加到该锚点的class属性中,我已经确认我的班级会产生预期的结果。 I have also confirmed that nav.Ctrl.checkCurrent('home') evaluates to true when I've clicked this link. 我还确认了当我单击此链接时nav.Ctrl.checkCurrent('home')计算结果为true

However, that being said...this directive fails to produce the results I'm looking for. 但是,话虽这么说...该指令无法产生我想要的结果。

The intended function is that when the function is true, the active class will be applied to my anchor tag. 预期的功能是,当该函数为true时, active类将应用于我的锚标签。

Since I have checked that the class works, and the function produces the correct boolean operator, I must have the syntax for ng-class incorrect. 由于我已经检查了该类是否正常工作,并且该函数生成了正确的布尔运算符,因此我必须对ng-class的语法不正确。 However, I recently completed a tutorial on Code School dealing with Angular JS and they use the following syntax... 但是,我最近完成了有关Angular JS的Code School教程,并且它们使用以下语法...

ng-class="{ active: tab.isSet(1) }"

...in an anchor tag. ...在锚标签中。 When the function tab.isSet(1) evaluates to true the class is applied to the element. 当函数tab.isSet(1)计算为true时,该类将应用于元素。

Why is my equal syntax not producing the same result? 为什么我的相等语法不能产生相同的结果?

也许您错过了结尾的“}”?

<a href="#/home" ng-class="{ active: navCtrl.checkCurrent('home') }" ng-click="navCtrl.setLink('home')" class="navigation-anchor">Home</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM