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

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

我正在尝试根据 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/

如果您绝对肯定必须使用条件子句而不是映射查找,请调用类似 f.e title="{{getTitle(t.direction)}}

的方法
$scope.getTitle = function(direction) {
    if (direction==="positive") {
        return "Increase";
    } else if (direction==="negative") {
        return "Decrease";
    }
};

http://jsfiddle.net/p6aysky5/

虽然我真的不明白这样做有什么意义。它所做的只是让你的代码更混乱。