我想应用ng-class来评估<中的负值。 p>标签
<p <strong>LW$:</strong> {{d.lw_metric}} <strong>LW:</strong> {{d.lw_metric_percentage}} <strong>L4W:</strong> {{d.l4w_metric}}</p>
我有我的CSS:
.positive{ color: green}
.negative{ color: red}
但我不知道怎么告诉角度来评估标签内的所有值而不是像这样一个一个地做
ng-class = "{'positive':data.merchMetrics.LW_CHG_LY >=0,'negative':data.merchMetrics.LW_CHG_LY <0}"
因为我在这个标签里面有五个表达式,所以我想有一些方法可以避免这种情况。
答案 0 :(得分:1)
您可以简单地将该逻辑转移到控制器
ng-class="evaluateClass(data.merchMetrics)"
控制器
$scope.evaluateClass = function(merchMetrics){
var returnClass = "";
//below for loop will iterate through each property of object
//and will decide the class need to apply
for (var key in merchMetrics) {
if (merchMetrics.hasOwnProperty(key)) {
//you could also add the logic here
//so that string will have positive & negative class once
if(merchMetrics[key] > 0)
returnClass = returnClass + "positive "
else
returnClass = returnClass + "negative "
}
}
//you could return your class from here
return returnClass;
};