AngularJS ng-repeat filtering with an OR-statement

Richard Lilja picture Richard Lilja · Aug 17, 2013 · Viewed 12.9k times · Source

So I have a dataset of companies stored in a variable in a controller.

$scope.companies = [
    {
        name: "first company",
        type: ["a", "b"]
    },
    {
        name: "second company",
        type: ["b"]
    },
    {   
        name: "third company",
        type: ["c"]
    }
]

Then i have my list where I want to list all companies of either a or b. I thought sending and array would work as an or-statement. Turns out it's more like an and-statement.

<ul>
    <li ng-repeat="company in companies | filter:filters{type: ['a', 'b']}">
        {{company.name}}
    </li>
</ul>

The code above would list "first company" while I would like it to list both the "first company" and the "second company". I know I could manipulate the $scope.companies dataset from within the controller, but I'd like to know if there's any "native" way to achieve this first.

Best regards! // Richard

Answer

basarat picture basarat · Aug 17, 2013

Your only options is to use a filter using a function:

<ul>
    <li ng-repeat="company in companies | filter:customFunction">
        {{company.name}}
    </li>
</ul>

Where

$scope.customFunction = function(item){ /* true if you want item, false if not */ }

Source: http://docs.angularjs.org/api/ng.filter:filter#parameters