2013-11-26 44 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); 
    } 
});