Javascript 在这种情况下,如何使用ng if或其他表达式更改文本?
我正在尝试根据Javascript 在这种情况下,如何使用ng if或其他表达式更改文本?,javascript,angularjs,angularjs-ng-repeat,angularjs-ng-if,Javascript,Angularjs,Angularjs Ng Repeat,Angularjs Ng If,我正在尝试根据标记的方向值更改标题标记中的文本: <li ng-repeat="t in tags"> <div class="tag" ng-class="{'green-up': t.direction == 'positive', 'red-down': t.direction == 'negative', '' : t.direction == 'st
标记的方向值更改标题标记中的文本:
<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>
{{t.name}}
方向为正
、负
或停滞
,应对应于title=“百分比增加”
title=“百分比减少”
或仅title=“百分比”
在这种情况下,最好使用哪种角度语法?为什么不将标记设置设置为对象
$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>
不过,我真的不明白这么做有什么意义。它所做的一切就是让你的代码更加混乱。我会考虑制作一个哈希表,并使用title={{{t.hashes[t.direction]}
但这并不完全是“角度”的,是这样做的,只是不想在模型中添加更多内容。。。性能(100个标签)。我想标记中的某种条件可能需要更少?我会再等一会儿,但如果没有其他问题,我会检查这个,然后将我的标题设置为justpercentage
我看不出有理由不使用可映射对象。你有没有试过,并注意到一个性能的打击?有比省吃俭用“正确”的方法更好的方法来提高性能ng如果
不起作用,它所做的就是从DOM中添加或删除整个元素。如果希望在标题中包含条件子句,则仍然需要调用范围中某个地方可访问的方法。更新答案。
$scope.getTitle = function(direction) {
if (direction==="positive") {
return "Increase";
} else if (direction==="negative") {
return "Decrease";
}
};