我有产品阵列。我想过滤类型键。我想一起只展示蔬菜和水果。 如何在AngularJS中设置单键和多值的过滤。
<div ng-controller="MyCtrl">
<ul>
<li ng-repeat="item in products | filter:({type:'vegetable'}||{type:'fruit'})">{{item.name}}</li>
</ul>
</div>
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.products = [
{name:"Apple",type:"fruit"},
{name:"Grape",type:"fruit"},
{name:"Orage",type:"fruit"},
{name:"Carrot",type:"vegetable"},
{name:"Milk",type:"dairy"}
]
}
答案 0 :(得分:0)
最简单的解决方案是在控制器中添加一个过滤器并使用:
$scope.filterFruitsAndVegies = function (item) {
return item.type === 'fruit' || item.type === 'vegetable';
};
<div ng-controller="MyCtrl">
<ul>
<li ng-repeat="item in products | filter: filterFruitsAndVegies">{{item.name}}</li>
</ul>
</div>