使用数组中的值过滤ng-repeat

时间:2014-03-19 19:51:50

标签: angularjs

我的这张表有一个ng-repeat。

ng-repeat="project in projects"

我在项目prj_city中有一个属性。我想过滤这个值。

我可以这样做:

ng-repeat="project in projects | filter={prj_city: <value>}

但是我希望<value>是一个包含多个城市而不是字符串的数组。有没有简单的方法可以做到这一点,还是我必须在我的控制器中手动执行此过滤器?

3 个答案:

答案 0 :(得分:0)

您需要在控制器上创建过滤功能。类似的东西:

$scope.filteredCities = function(city) {
    return ($scope.userFilteredCities.indexOf(city) !== -1);
};

$scope.userFilteredCities;//List of filtered cities

答案 1 :(得分:0)

很可能控制器中的自定义过滤器应该很容易:

var filteredCities = ["LosAngelos", "etc.."];

$scope.arrayFilter = function(project) {
    for (var i = 0; i < filteredCities.length; i++) {
        if (filteredCities[i] == project.prj_city)
            return true;
    }
    return false
}

电话:

ng-repeat="project in projects | filter: arrayFilter"

答案 2 :(得分:0)

在控制器中定义以下功能:

// use a map for faster filtering
var acceptedCityMap = {};
angular.forEach(acceptedCities, function(city) {
    // case insensitive search. But you're not forced to
    acceptedCityMap[city.toLowerCase()] = true;
});

$scope.isProjectedAccepted = function(project) {
    // case insensitive search. But you're not forced to
    return acceptedCityMap[project.prj_city.toLowerCase()];
}

然后在你看来:

ng-repeat="project in projects | filter:isProjectAccepted"