Search code examples
javascriptangularjsangularjs-ng-repeatangular-filters

filter with multiple values of same properties of an object


I have an object called tasks. It has various properties. Now I want to display only the tasks which's status is to-do and in-progress. Please help me how can I apply the filter in ng-repeat.

$scope.tasks = [
 {
   'title': 'task one',
   'status': 'to-do'
 },

 {
   'title': 'task two',
   'status': 'in-progress'
 },

 {
   'title': 'task three',
   'status': 'completed'
 }

]

<div ng-repeat="task in tasks">
    <h1>{{task.name}}</h1>
    <p>{{task.status}}</p>
</div>

Solution

  • you need to specify a custom function to the filter. here's a working plnkr

    code:

    <div ng-repeat="task in tasks | filter:status">
        <h1>{{task.name}}</h1>
        <p>{{task.status}}</p>
    </div>
    

    js:

    $scope.status = function(item) {
      if (item.status == 'to-do' || item.status == 'in-progress') return true;
    }