angularjs中判断ng-repeat是否迭代完的实例
angular中的ng-repeat指令会自动迭代数组然后你就可以将这些迭代的数据在页面逐条显示。但是怎样才知道数据迭代完了,其实还是有方法的,今天就介绍一种方法来判断ng-repeat是否已经迭代完。众所周知ng-repeat会为每一个repeat的对象创建一个控制器,我们就利用这个来判断。
首先我们要定义一个数组
$scope.testArrray = [
{id: 1, value: 1},
{id: 2, value: 2},
{id: 3, value: 3},
{id: 4, value: 4},
{id: 5, value: 5},
{id: 6, value: 6},
{id: 7, value: 7},
{id: 8, value: 8}];然后我们repeat这个数组在html页面中使用ng-repeat指令,并为他们创建控制器。
<body ng-controller="app1Controller">
<content>
<!--为每一个repeat的对象创建一个itemRepeatCtrl控制器-->
<div class="item" ng-controller="itemReaptCtrl" ng-repeat="li in testArrray track by li.id">
{{li.value}}
</div>
</content>
</body>接着就是为他们创建子控制器
.controller('itemReaptCtrl', ['$scope', function ($scope) {
$scope.$watch($scope.$last, function () {
console.log("执行了一次!");
if($scope.$last){ //$scope.$last是来判断是否是最后一个ng-repeat对象, 如果是则$scope.$last的值为true ,反之则为false
$scope.$emit('ngRepeatFinished'); // 由于是向父控制器中发布广播,所有用$emit
}
})
}])然后在父控制器中接受广播
$scope.$on('ngRepeatFinished', function (data) { //接收广播,一旦repeat结束就会执行
console.log("恭喜你,repeat结束了!");
});看控制器中有打印,证明此方法有效

有时候需要在ng-repeat 指令repeat结束之后进行一些操作时便可以使用这种方法。
相关推荐
CrazyDogWang 2017-02-14
zbwroom 2016-09-23
youyouzouzou 2015-11-30
princejingyun 2013-01-30
Oranq 2013-09-24
闲听风月 2018-01-16
forthemyth 2017-09-18
codergirl 2017-07-06
Inuyasha 2017-01-21
forthemyth 2017-01-21
鸟菜小端前 2017-01-19
thesameguo 2016-12-31
itseebin 2019-04-29
DataCastle 2016-12-13
鸟菜小端前 2016-11-02
codergirl 2019-04-29
鸟菜小端前 2019-04-26
Inuyasha 2016-08-23
雅特岚蒂斯 2019-04-25