我正在尝试根据tag
的direction
值更改标题标签中的文本:
<li ng-repeat="t in tags">
<div class="tag"
ng-class="{'green-up': t.direction == 'positive',
'red-down': t.direction == 'negative',
'' : t.direction == 'stagnant'}"
title="Percentage increase"
ng-if="">{{t.name}}</div>
</li>
方向为positive
、negative
或stagnant
,对应title="Percentage increase"
、title="Percentage decrease"
或仅对应title="Percentage"
为什么不将标签设置为对象
$scope.tagInfo = {
positive:{ class:"green-up", title:"Percentage increase" },
negative:{ class:"red-down", title:"Percentage decrease" }
}
然后在ng-repeat
中调用它<div class="tag"
ng-class="tagInfo[t.direction].class"
title="{{tagInfo[t.direction].title}}">{{t.name}}</div>
http://jsfiddle.net/rLrh0Lr1/如果您绝对必须使用条件子句而不是映射查找,则调用如下方法:e title="{{getTitle(t.direction)}}
$scope.getTitle = function(direction) {
if (direction==="positive") {
return "Increase";
} else if (direction==="negative") {
return "Decrease";
}
};
http://jsfiddle.net/p6aysky5/我真的不明白这样做有什么意义。