角度$ filter过滤负数,即使过滤器为正数

时间:2018-11-12 23:11:45

标签: angularjs filter negative-number

我有一个简单的数组过滤器。数组中的每个项目都有一个“ Rating”属性,该属性是一个数字。值可以是-3,-2,-1,1,2,3。

如果我这样做

$scope.ratingFilter = -3; // negative numbers work fine

但我愿意

$scope.ratingFilter = 3; // positive numbers are showing items that have a Rating of 3 AND -3. Why are negative number appearing in the filter???

$scope.updateMarkers = function(){

    var filters = [];
    if($scope.ratingFilter != 'all'){
        filters.Rating = $scope.ratingFilter;
    }

    var arr = [];
    angular.forEach($scope.allMarkers, function(value, key){
        arr.push(value);
    });

    $scope.markers = $filter('filter')(arr, filters);
}

3 个答案:

答案 0 :(得分:0)

考虑使用普通的JavaScript在控制器中自己过滤数组:

$scope.markers = arr.filter(x => x.Rating === ratingFilter);

答案 1 :(得分:0)

true中将'strict'值设置为$filter

$filter('filter')(arr, filters, true);

请参阅https://docs.angularjs.org/api/ng/filter/filter

答案 2 :(得分:0)

$ scope.dsset = $ filter('filter')($ scope.array,{id:1});