我有以下代码:
<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 ...
最佳答案
可以使用 ng-class
并在 css 规则中设置颜色
<span ng-bind="status"
ng-class="{'green-class': status=='OK', 'red-class': status=='NOT OK'}" ></span>
或者使用ng-style
做类似的事情
关于javascript - 如何用相似的元素简化 ng-switch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31931489/