marceloduende
marceloduende

Reputation: 719

if statement within a ng-repeat directive on Angular.js

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.

Upvotes: 15

Views: 14203

Answers (2)

Jan.J
Jan.J

Reputation: 3080

As I wrote in a comment, you could use filters to achieve that. Here's example: http://jsfiddle.net/sebmade/ZfGx4/44/

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


And filter code:

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


Edit:
I found that it is possible to do it with inline filter like this:

ng-repeat="list in lists | filter:{selected: true}"

Upvotes: 34

Shai Reznik - HiRez.io
Shai Reznik - HiRez.io

Reputation: 8948

What you need to add here is a filter:

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

I've added a plnkr as an example.

Upvotes: 10

Related Questions