angularjs中的$ watchGroup无效

时间:2016-12-11 07:12:15

标签: angularjs

我正在使用ng-repeat来显示数组中的对象项,我也使用了分页过滤器。 在下面我使用$ watch来监视Filter.type.car并且它的工作正常,但是当我尝试使用$ watchGroup一起观看Filter.type.car和Filter.type.Motorbike时,它现在正在工作。

$scope.currentPage = 0;
$scope.pageSize = 10;  
$scope.getData = function () {
    return $filter('filter')($scope.vichicles);
};
$scope.numberOfPages=function(){
    return Math.ceil($scope.vichicles/$scope.pageSize);                
};
$scope.$watchGroup(['Filter.type.car','Filter.type.motorbike'], function(term){
    $scope.filtered = filterFilter($scope.vichicles, term);
    $scope.getData = function () {
        return $filter('filter')($scope.filtered)
    };
    $scope.numberOfPages=function(){
        return Math.ceil($scope.getData().length/$scope.pageSize);                
    };  
    $scope.totalItems = $scope.filtered.length;
});

这是视图

<div class="pagination">
<div class="pager">
<span class="current-page">{{currentPage+1}}</span>
<span class="total-pages">{{numberOfPages()}}</span>
</div>
<span class="total-items">{{totalItems}}</span>
<span class="item-description">Vichicles</span>
<button class="btn btn-clear arrow arrow-up" ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">Previous</button>
<button class="btn btn-clear arrow arrow-down" ng-disabled="currentPage >= getData().length/pageSize - 1" ng-click="currentPage=currentPage+1">Next</button>
</div>
<ul class="list">
<li ng-repeat="v in vichicles | filter: search |searchFilter:Filter.type  | searchFilter:Filter.level| startFrom:currentPage*pageSize | limitTo:pageSize">

1 个答案:

答案 0 :(得分:1)

使用单独的Watch而不是watchGroup,因为它们不是数组

 $scope.$watch('Filter.type.car', function() {
        alert('hey, car has changed!');
    });

 $scope.$watch('Filter.type.motorbike', function() {
        alert('hey, bike has changed!');
    });