如何简化类似元素的ng-switch?

时间:2015-08-11 00:52:29

标签: javascript angularjs ng-switch ng-bind

我有以下代码:

<span ng-switch="status">
    <span ng-switch-when="NOT OK">
        <span style="color: red;" ng-bind="status"></span>
    </span>
    <span ng-switch-when="OK">
        <span style="color: green;" ng-bind="status"></span>
    </span>
    <span ng-switch-default>
        <span ng-bind="status"></span>
    </span>
 </span>

有什么方法可以优化此代码吗?我想我有一些重复的ng-binding ...

1 个答案:

答案 0 :(得分:2)

可以使用ng-class并在css规则中设置颜色

<span ng-bind="status" 
      ng-class="{'green-class': status=='OK', 'red-class': status=='NOT OK'}" ></span>

或使用ng-style

进行类似操作