2013-11-26 45 views
3

我有以下ng重复?我怎样才能看过滤ng-repeat中元素的数量?

<table> 
<tr data-ng-repeat="row in grid.data | filter:isProblemInRange> 
<td>xxx</td> 
</tr> 
</table> 

行使用基于两个输入字段的值的isProblemInRange函数进行过滤。根据我在这些字段中显示的行数可以改变。

是否有可能对屏幕上显示的行数进行监视,如果有,我该如何从控制器内部执行此操作?

回答

0

你可以在你的控制器使用$filter service

var $scope.filteredRows = $filter('filter')($scope.gridData, $scope.isProblemInRange); 

,并通过过滤行运行NG重复。通过这种方式,您可以避免在模板中引入新的范围字段,从而使您的代码难以测试和维护。

查看this plunk的例子。

0
var $scope.filteredRows = $filter('filter')($scope.gridData, $scope.isProblemInRange); 

$scope.$watch('filteredRows',function(newVal,oldVal){ 
    if(!angular.equals(newVal.length,oldVal.length)){ 
     [... Do stuff here when the number of filtered rows changes ...] 
    } 
}); 

$scope.$watch('gridData',function(newVal,oldVal){ 
    if(!angular.equals(newVal,oldVal)){ 
     $scope.filteredRows = $filter('filter')($scope.gridData, $scope.isProblemInRange); 
    } 
});