AngularJS ng-repeat filtering with OR-statement

So, I have a set of company data stored in a variable in the controller.

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

Then I have a list where I want to list all companies either a or b. I thought sending and array would work as or-statement. It turns out this is more like an and-statement statement.

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

The code above will indicate “first company”, while I would like it to display both “first company” and “second company”. I know that I can manipulate the $ scope.companies dataset from the controller, but I would like to know if there is any “native” way to achieve this.

Yours faithfully! // Richard

+6
source share
1 answer

Your only options are to use a filter using the 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

+12
source

Source: https://habr.com/ru/post/951925/


All Articles