过滤AngularJS中一个键的多个值

时间:2014-12-27 07:26:24

标签: angularjs angularjs-directive angularjs-ng-repeat

我有产品阵列。我想过滤类型键。我想一起只展示蔬菜和水果。 如何在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"}
        ]
    }

1 个答案:

答案 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>

http://plnkr.co/edit/HhZ49dpylTRJMVnSKdyt?p=preview