15

I have an ng-repeat li elements and I want to set a specific value for that li depending on whether a function returns true or false and I don't want to do it in the controller because I have dependency issues where it makes it Minimize or Maximize for ALL li elements but I want it to be for each individual li element. I tried using several things including the following without any luck. I am not sure what I am doing wrong but I would appreciate any input or any other way of doing this.

object1 is from an ng-repeat that includes this li element.

<li><a tabindex="-1" ng-click="setHidden(object1)">{isItHidden(object1) ? 'Minimize' : 'Maximize'}</a></li>

SOLUTION (thanks to jdp)

<li><a tabindex="-1" ng-click="setHidden(object1)">{{isItHidden(object1)}}</a></li>

$scope.isItHidden = function(object){
    return object.hidden ? 'Maximize' : 'Minimize';
}
4

2 回答 2

33

随着 Angular 1.2 的发布,您现在可以在内部使用更直观的三元运算符ng-bind

<a ng-bind="object1.hidden ? 'Maximize' : 'Minimize'"></a>

或括号内

<a>{{ object1.hidden ? 'Maximize' : 'Minimize' }}</a>
于 2014-06-09T14:42:54.137 回答
12

你可以这样做

{{ object1.hidden && 'Minimize' || 'Maximize' }}

在 Angular 1.2 中,将添加一个真正的三元运算符。

于 2013-07-16T19:00:58.880 回答