如何修复过滤Angular JS?

时间:2017-03-13 11:52:20

标签: javascript angularjs

当对象的元素存在于数组中时,我的过滤器应返回对象中的项:

$scope.isCategory = function() {
    return function(item) {
        if($scope.filterObj.categories.length > 0) {
            return angular.forEach( $scope.filterObj.categories, function(value, key) {
                return (item.categories.indexOf(value) == -1);
            });
        }
        return item;
    }
};

其中$scope.filterObj是[“a”,“b”]而item.categories是[“a”]。

1 个答案:

答案 0 :(得分:1)

尝试:

$scope.isCategory = function() {
    return function(item) {
        if($scope.filterObj.categories.length > 0) {
            var found = false;
            angular.forEach($scope.filterObj.categories, function(value, key) {
                if (!found && item.categories.indexOf(value) !== -1) {
                    found = true;
                }
            });

            return found ? item : null;
        }
        return item;
    }
};