在一个领域的多个值AngularJS过滤器 [英] AngularJS filter on multiple values of one field

查看:152
本文介绍了在一个领域的多个值AngularJS过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够对一个对象的一个​​场的两个不同的值进行过滤。看看下面的例子。

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"}
]

通过过滤 NG-重复=,在产品项目|过滤器:{类型:'果'}。我可以得到所有的水果。但是,如果我想要得到所有的水果和蔬菜。我试过

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例

JSFiddle Example

推荐答案

使用过滤函数:

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

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

小提琴

Fiddle

这篇关于在一个领域的多个值AngularJS过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆