我需要能够对对象的一个字段的两个不同的值进行过滤.举个例子.
$scope.products = [
{name:"Apple",type:"fruit"},{name:"Grape",{name:"Orage",{name:"Carrot",type:"vegetable"},{name:"Milk",type:"dairy"}
]
使用过滤器ng-repeat =“products in products | filter:{type:’fruit’}”.我可以得到所有的水果.但是如果我想要得到所有的水果和蔬菜怎么办?我试过了
ng-repeat="item in products | filter:{type:['fruit','vegetable']}"
但那没有办法.我认为应该有一个简单的解决方案,但我找不到.
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