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.
You can add condition using ng-if also and this is effective too. You can apply any condition to your list using ng-if attribute. In below example, I have put condition where Age > 20 and IsActive is true. ng-repeat will fetch all records which full fill this scenario.
AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.
Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.
AngularJS ng-if Directive The ng-if directive removes the HTML element if the expression evaluates to false. If the if statement evaluates to true, a copy of the Element is added in the DOM.
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}"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With