缩短ng-class值

时间:2014-11-21 16:13:40

标签: angularjs ng-class

有没有更简洁的方法来写这个:

ng-class="{ 'red': (percent < 0), 'green': (percent > 0), 'glyphicon-arrow-down': (percent < 0), 'glyphicon-arrow-up': (percent > 0) }"

我发现自己写了很多这些过长的ng-class属性表达式。

2 个答案:

答案 0 :(得分:0)

您可以组合相似条件的类别。

ng-class="{ 'red glyphicon-arrow-down': (percent < 0), 'green glyphicon-arrow-up': (percent > 0) }"

答案 1 :(得分:0)

可能会删除括号:

ng-class="{ 'red': percent < 0, 
            'green': percent > 0, 
            'glyphicon-arrow-down': percent < 0, 
            'glyphicon-arrow-up': percent > 0 }"