angularjs apply one class by ternary and another always

隐身守侯 提交于 2019-12-11 03:11:38

问题


I want to have the 'glyphicon' class on my icon tag always. I want to have the 'glyphicon-plus' class when item.expanded is false, 'glyphicon-minus' class when item.expanded is true.

It seems there would be a simple way to do this but I've been unsuccessful using the ternary operator when I need another class to always be there. The following code does not work.

<i ng-class="'glyphicon', (item.expanded) ? 'glyphicon-minus' : 'glyphicon-plus'"></i>

回答1:


ng-class don't really behave like this.

google says : What is the best way to conditionally apply a class?

so an answer for your question could be :

<i class="glyphicon" ng-class="{'glyphicon-minus' : item.expanded, 'glyphicon-plus' : !item.expanded}"></i>


来源:https://stackoverflow.com/questions/22940041/angularjs-apply-one-class-by-ternary-and-another-always

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!