Tony
Tony

Reputation: 12695

AngularJS - filter array item based on its array property

I have that code:

<label>Ages: <input type="text" ng-model="objProp2AgesFilter" /></label><br />
<table>
  <thead>
     <tr>
       <th>Name</th>
     </tr>
  </thead>
  <tbody>
     <tr ng-repeat="p in items">
       <td>{{p.name}}</td>
     </tr>
  </tbody>
</table>

items array looks like:

[{ 
  name: 'tst1',
  ages: [1,2,3]
},{ 
  name: 'tst2',
  ages: [2,3,4]
}]

How to filter that items, whose contain value e.g. 4 in the ages array ?

Upvotes: 0

Views: 27

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

<tr ng-repeat="p in items | filter:hasAge4">

And in the controller:

$scope.hasAge4 = function(item) {
    return item.ages.indexOf(4) >= 0;
};

Upvotes: 3

Related Questions