我試圖在過濾的集合發生更改時發生事件觸發。已過濾的列表以ng-repeat附加到未過濾的列表。如何在Angular.JS中觀看已過濾的集合?
<tr ng-repeat="item in $scope.filtered = (vm.list | filter:vm.searchText) | limitTo:vm.limit:vm.begin">
這是我的活動我要火:
$scope.$watchCollection('filtered', function() {
alert($scope.filtered.length);
}, true);
它觸發一次當第一次加載頁面,在我的Ajax調用填充vm.list,因此警報說0,但隨後應該在vm.list被填充後再次觸發,並且每次對vm.searchText進行更改都會導致對$ scope.filtered的更改,但事實並非如此。
我也試圖使$ watchCollection方法是這樣的:
$scope.$watchCollection('filtered', function (newList, oldList) {
alert(newList.length);
});
但是,有同樣的結果。
我也想這樣做的建議here,它弄成這個樣子:
<tr ng-repeat="item in catchData((vm.list | filter:vm.searchText)) | limitTo:vm.limit:vm.begin">
$scope.catchData = function (filteredData) {
alert(filteredData.length);
return filteredData;
}
這似乎像它在第一次固定它。它現在在API調用填充列表時觸發,並且每當searchText導致過濾列表更改時再次觸發。不幸的是,它改變了limitTo過濾器上的begin選項不再起作用。更改限制選項仍然有效,但不是開始。更改開始仍然可以使用$ watchCollection方法。
有沒有人有任何想法?
太開始的手錶,你需要消化()因爲你正在使用警報,你必須調用apply()方法手動這將導致digest()運行,因此你的觀察者。 –
@AayushiJain請引用文檔。從未在手錶中使用'$ apply()'或'$ digest()'來處理角度範圍內的任何事情 – charlietfl
'$ scope.filtered'中的項目無效......視圖中沒有'$ scope' ...在vm.filtered =(....)中是'item並且在使用'controllerAs'時需要使用函數 – charlietfl