使用带有OR的AngularJS $ filter('filter')

时间:2014-12-21 14:37:57

标签: angularjs angular-filters

我有这个对象

$scope.items = [
    {id:1, name:'John'},
    {id:2, name:'Steve'},
    {id:3, name:'Joey'},
    {id:4, name:'Mary'},
    {id:5, name:'Marylin'}];

我需要过滤两个值,如3,4,我正在尝试类似的东西

$scope.items2 =  $filter('filter')($scope.items, {id:3,4});

我该怎么做?需要使用自定义过滤器吗?

http://jsfiddle.net/cWVrV/23/

1 个答案:

答案 0 :(得分:2)

使用回调函数:

$scope.items2 =  $filter('filter')($scope.items, function(obj) {
    return obj.id == 3 || obj.id == 4;
});

演示:http://jsfiddle.net/cWVrV/24/

或者只是以同样的方式使用普通的Array.prototype.filter

$scope.items2 = $scope.items.filter(function(obj) {
    return obj.id == 3 || obj.id == 4;
});