AngularJS过滤器包含负数

时间:2015-04-13 08:00:35

标签: angularjs angularjs-filter

当我使用以下数字过滤对象数组时:

$scope.users = [{name:'John', id:-1},
                         {name:'Mary', id:1},
                         {name:'Mike', id:2},
                         {name:'Adam', id:3}];

var selected = $filter('filter')($scope.users, {id: $scope.target});

如果target = 1,则所选结果将包括John和Mary。但我只想要包含与id为1的Mary完全相同的id。为什么包含负数?我该如何防止这种情况?

1 个答案:

答案 0 :(得分:4)

您可以将比较器的第三个参数设置为真

$scope.users = [{name:'John', id:-1},
                         {name:'Mary', id:1},
                         {name:'Mike', id:2},
                         {name:'Adam', id:3}];

var selected = $filter('filter')($scope.users, {id: $scope.target}, true);

true是:

的简写
function(actual, expected) { 
    return angular.equals(actual, expected)
} // This is essentially strict comparison of expected and actual.

Plunkr