如何在这种情况下使用ng-if或其他表达式更改文本

How to change text in this case using ng-if or other expression?

本文关键字:表达式 其他 文本 ng-if 这种情况下      更新时间:2023-09-26

我正在尝试根据tagdirection值更改标题标签中的文本:

<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>

方向为positivenegativestagnant,对应title="Percentage increase"title="Percentage decrease"或仅对应title="Percentage"

在这种情况下,哪种Angular语法最好用?

为什么不将标签设置为对象

$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/

我真的不明白这样做有什么意义。

相关文章: