【发布时间】:2017-10-27 21:33:13
【问题描述】:
在 Angular 中,我需要根据不同的条件应用不同的样式属性,但它不能以这种方式工作,我只能通过条件表达式应用 2 个条件。
<div ng-style="
(status=="up") ?{'background-color':'green' ,'color':'green'}
(status=="down")?{'background-color':'red' ,'color':'red'}
(status=="idle")?{'background-color':'yellow','color':'yellow'}
(status=="") ?{'background-color':'grey' ,'color':'grey'}
">
如果您知道任何方法将属性传递给返回样式 obj 的 ng-style 函数的函数会更好,就像下面这样奇怪地不起作用!
$scope.styleFn(status,attr) {
(status=="up") ?{attr:'green'}
(status=="down")?{attr:'red'}
(status=="idle")?{attr:'yellow'}
(status=="") ?{attr:'grey'}
}
<div ng-style="styleFn('up','background-color')">
【问题讨论】:
标签: javascript css angularjs ng-style