15

I am trying to implement an if into a ng-repeat directive but I am having a hard time. my code which work for now is:

<p ng-repeat="list in lists">{{list[id].title}}</p>

What I want to do is basically

<p ng-repeat="list in lists if list[id].selected">{{list[id].title}}</p>

Of course, on the second line I am getting an error. Any advice on this?

Thank you.

4

2 回答 2

34

正如我在评论中所写,您可以使用过滤器来实现这一点。这是示例:http: //jsfiddle.net/sebmade/ZfGx4/44/

ng-repeat="list in lists | filter:myFilter"


和过滤代码:

$scope.myFilter = function(item) {
    return item.selected == true;
};


编辑:
我发现可以使用这样的内联过滤器来做到这一点:

ng-repeat="list in lists | filter:{selected: true}"
于 2013-02-06T22:05:21.300 回答
10

您需要在此处添加一个过滤器:

<p ng-repeat="list in lists | filter:{selected:true}">test {{list.title}}</p>

我添加了一个 plnkr 作为示例

于 2013-02-06T22:11:56.297 回答