admin管理员组文章数量:1279090
I have been looking for examples of multiple conditions in ng-class but I can't find using the same terminology. Here is an example of what I want to achieve.
ng-class="isSelected ? 'side_menu_link_active' : 'side_menu_link_disabled' , pageSelected =='lectures' ? 'link_active' : 'link_disabled'"
Does anyone knows the correct syntax for this? thanks.
I have been looking for examples of multiple conditions in ng-class but I can't find using the same terminology. Here is an example of what I want to achieve.
ng-class="isSelected ? 'side_menu_link_active' : 'side_menu_link_disabled' , pageSelected =='lectures' ? 'link_active' : 'link_disabled'"
Does anyone knows the correct syntax for this? thanks.
Share Improve this question asked Jan 28, 2015 at 18:03 Diogo BarrosoDiogo Barroso 9153 gold badges9 silver badges22 bronze badges 1- see my updated answer below. – SoluableNonagon Commented Jan 28, 2015 at 18:10
2 Answers
Reset to default 7If you want multiple conditions the syntax is:
ng-class=" {className: valueToCheckForTruthiness, otherClassName: otherValue } "
Example using your class names:
ng-class="{side_menu_link_active: isSelected, side_menu_link_disabled: !isSelected, link_active: pageSelected == 'lectures', link_disabled: pageSelected != 'lectures' }"
You can take this further and generate an object using a function:
$scope.classGenerator = function(isSelected, pageSelected){
var obj = {};
isSelected ? obj.side_menu_link_active=true : obj.side_menu_link_disabled = false;
pageSelected == 'lectures' ? obj.link_active = true : obj.link_disabled = false;
return obj;
}
ng-class="classGenerator(isSelected, pageSelected)"
You can do this by ma separation
<div ng-class="{'isClicked1' : click, 'isClicked2' :!click}">
See my working jsfiidle
本文标签: javascriptngClassmultiple conditions and classesStack Overflow
版权声明:本文标题:javascript - ng-Class - multiple conditions and classes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741218568a2360523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论