Angular.js ng-repeat filter by property having one of multiple values (OR of values)

147,933

Solution 1

Best way to do this is to use a function:

<div ng-repeat="product in products | filter: myFilter">

$scope.myFilter = function (item) { 
    return item === 'red' || item === 'blue'; 
};

Alternatively, you can use ngHide or ngShow to dynamically show and hide elements based on a certain criteria.

Solution 2

For me, it worked as given below:

<div ng-repeat="product in products | filter: { color: 'red'||'blue' }">

<div ng-repeat="product in products | filter: { color: 'red'} | filter: { color:'blue' }">

Solution 3

I thing ng-if should work:

<div ng-repeat="product in products" ng-if="product.color === 'red' 
|| product.color === 'blue'">

Solution 4

In HTML:

<div ng-repeat="product in products | filter: colorFilter">

In Angular:

$scope.colorFilter = function (item) { 
  if (item.color === 'red' || item.color === 'blue') {
  return item;
 }
};

Solution 5

Here is a way to do it while passing in an extra argument:

https://stackoverflow.com/a/17813797/4533488 (thanks to Denis Pshenov)

<div ng-repeat="group in groups">
    <li ng-repeat="friend in friends | filter:weDontLike(group.enemy.name)">
        <span>{{friend.name}}</span>
    <li>
</div>

With the backend:

$scope.weDontLike = function(name) {
    return function(friend) {
        return friend.name != name;
    }
}

.


And yet another way with an in-template filter only:

https://stackoverflow.com/a/12528093/4533488 (thanks to mikel)

<div ng:app>
  <div ng-controller="HelloCntl">
    <ul>
       <li ng-repeat="friend in friends | filter:{name:'!Adam'}">
            <span>{{friend.name}}</span>
            <span>{{friend.phone}}</span>
        </li>
    </ul>
</div>

Share:
147,933
Yogesh Mangaj
Author by

Yogesh Mangaj

Full stack engineer with 8 years experience working on core logistics problems and building a big data wrangling and analytics SaaS platform from the ground up Diverse experience spanning Engineering craftsmanship (building core backend modules, leading frontend development), Product development (managing requirements, UX reviews) and Team growth (mentorship, hiring &amp; refining development processes)

Updated on January 20, 2020

Comments

  • Yogesh Mangaj
    Yogesh Mangaj over 4 years

    Is it possible to filter an array of objects, such that the value of property can be either of a few values (OR condition) without writing a custom filter

    This is similar to this problem - Angular.js ng-repeat :filter by single field

    But instead of

    <div ng-repeat="product in products | filter: { color: 'red' }">
    

    is it possible to do something like this

    <div ng-repeat="product in products | filter: { color: 'red'||'blue' }">
    

    for a sample data as follows-

    $scope.products = [
       { id: 1, name: 'test', color: 'red' },
       { id: 2, name: 'bob', color: 'blue' }
       /*... etc... */
    ];
    

    I've unsuccessfully tried

    <div ng-repeat="product in products | filter: { color: ('red'||'blue') }">