应用ng类来计算P标记内的不同表达式
Apply ng-class to evaluate different expressions inside a P tag
我想应用一个ng类来计算<标记
<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}
但我不知道如何告诉angular评估标签内的所有值,而不是像那样一个接一个地评估
ng-class = "{'positive':data.merchMetrics.LW_CHG_LY >=0,'negative':data.merchMetrics.LW_CHG_LY <0}"
因为我在这个标签里有五个表达式,所以我想有一些方法可以避免这种情况。
您可以简单地将该逻辑转移到控制器
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;
};
相关文章:
- ng init中的表达式无法使用ng repeat
- Angular,表达式{{}}的结果没有插入到ng-click中
- ng disabled在放入多个表达式时失败
- ng类应用于页面加载,表达式不真实
- 在 ng-include 中使用变量作为表达式
- AngularJS-在$event.stopPropagation之后停止ng-click表达式求值
- ng init与razor表达式
- 未为text/ng模板计算角度表达式
- AngularJS ng显示表达式在加载时不起作用
- 使用ng repeat中的键值来扩展作用域表达式
- IE支持多个Angular ng类表达式
- Angular js ng show没有'无法使用本机数学表达式
- ng-class 的函数表达式在 ng-click 时被调用
- 不适用于 Angular ng 禁用中的表达式
- 如何将 ng-click 与多个表达式一起使用
- 计算 ng-repeat Angular JS 表达式的总数
- AngularJS ng-pattern的正则表达式问题
- AngularJS ng-pattern 函数,带有正则表达式和年份输入字段的日期
- AngularJS在评估正则表达式ng模式时出现问题
- angularjs中的表达式ng按键事件意外结束