2015-12-02 61 views
1

我有对象数组。当我需要筛选单vaue对象我这样做是这样的:如何过滤具有多个值的对象?

$scope.filteredByPhase = $filter('filter')($scope.allApps, {Phase:"All"}); 
$scope.allAppsBatch = $scope.filteredByPhase; 

但作为一个选择,我想通过在‘全部’或‘家’,以过滤由2“Phase`值的对象这种情况下如何过滤?

我想是这样的:

$scope.filteredByPhase = $filter('filter')($scope.allApps, {Phase:("All" || "Home")}); 
$scope.allAppsBatch = $scope.filteredByPhase; 

但不是工程..任何一个指导我好吗?

回答

2

在AngularJS中,您可以使用函数作为过滤器中的表达式。在该函数中,您可以验证条件并返回布尔值。所有falsy项目都被滤除了结果。所以,你可以做

$scope.filteredByPhase = $filter('filter')($scope.allApps, function (app) { 
    if (app.Phase == "All" || app.Phase == "Home") { 
     return true; 
    } 
    return false; 
}); 

阅读更多:AngularJS Filter Documentation

0

使用$过滤器通过一个匿名比较函数。

$scope.filteredItems = $filter('filter')($scope.items, function (item) { 
    return (item.Phase == "all") ? true : false; 
}); 

请记住,你可以使用Array.filter还有:

$scope.items = [{ 
    Phase: "home" 
}, { 
    Phase: "all" 
}, { 
    Phase: "all" 
}, { 
    Phase: "home" 
}]; 
console.log($scope.items); 
$scope.filteredItems = $scope.items.filter(function (item) { 
    return (item.Phase == "all") ? true : false; 
}) 
console.log($scope.filteredItems) 

您也可以使用触发链接多个过滤操作:

$scope.fi = $scope.i.filter(func1).filter(func2); 
相关问题