12

I need to be able to filter on two different values of one field of an object. Take the following example.

$scope.products = [
    {name:"Apple",type:"fruit"},
    {name:"Grape",type:"fruit"},
    {name:"Orage",type:"fruit"},
    {name:"Carrot",type:"vegetable"},
    {name:"Milk",type:"dairy"}
]

With the filter ng-repeat="item in products | filter:{type:'fruit'}". I can get all of the fruits. But what if I want to get all of the fruits and vegetables. I tried

ng-repeat="item in products | filter:{type:['fruit','vegetable']}"

But that didn't work. I figure there should be a simple solution to this, but I couldn't find it.

JSFiddle Example

2 Answers 2

23

Use a filter function instead:

$scope.fruitOrVeg = function(product){
    return product.type == 'fruit' || product.type == 'vegetable';
};

<li ng-repeat="item in products | filter:fruitOrVeg">{{item.name}}</li>

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

The correct syntax for AngularJs 1.8 is

| filter:{property:value1}||{property:value2}

for example

| filter:{someIntegerVariable:1}||{someIntegerVariable:2}

| filter:{someStringVariable:'this'}||{someStringVariable:'that'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.