Javascript ng重复最终回调事件

Javascript ng重复最终回调事件,javascript,angularjs,Javascript,Angularjs,我需要编写一个回调方法,每次调用ng repeat时都会触发该方法 从网上得到了一些选择 一,。ng重复上的ng初始化 这里使用ng init属性调用该方法 <ol ng-repeat="record in records | orderBy: sortCol:true" ng-init="$last && callMethod()"> 但我的问题是,它们只会在初始时触发一次(正如名称nginit所表明的那样)。我希望在sortColumn或sortOrder(as

我需要编写一个回调方法,每次调用ng repeat时都会触发该方法

从网上得到了一些选择

一,。ng重复上的ng初始化

这里使用ng init属性调用该方法

<ol ng-repeat="record in records | orderBy: sortCol:true" ng-init="$last && callMethod()">
但我的问题是,它们只会在初始时触发一次(正如名称nginit所表明的那样)。我希望在sortColumn或sortOrder(asc或desc)或数据(记录)发生更改时调用该方法。但是以上两种方法都不能解决我的问题


非常感谢您对记录更改的帮助。你尝试过使用$watch方法吗

$scope.$watch('records.length', function(newArray, oldArray){
    //Do something
},true);

由于watch only watches for reference change,我认为您的解决方案应该是$watchCollection()

我已经用以下代码解决了这个问题:

HTML:

<ol ng-repeat="record in ($parent.newItems = (records | orderBy: sortCol:true))">
$scope.$watchCollection('newItems ', function(newCollection, oldCollection) {
        newCollection && newCollection.length>0 && $scope.doSomething();
});     

orderBy不会更改原始列表的长度或列表的顺序。那么手表将如何触发事件..您可以使用“true”参数制作一个深度手表(像object中的object),如下所示:scope.$watch('name',function(newValue,oldValue){…},true);watchCollection用于监视scopewatch中的多个变量,而使用True param的watch要昂贵得多。
<ol ng-repeat="record in ($parent.newItems = (records | orderBy: sortCol:true))">
$scope.$watchCollection('newItems ', function(newCollection, oldCollection) {
        newCollection && newCollection.length>0 && $scope.doSomething();
});