angular指令监听ng-repeat渲染完成
2017-09-09 11:23
721 查看
今天工作中遇到需要用到ng-repeat遍历渲染完后执行某个操作,angular本身并没有提供监听ng-repeat渲染完成的指令,所以需要自己创建自定义指令。
在ng-repeat模板实例内部会暴露出一些特殊属性$index/$first/$middle/$last/$odd/$even,$index会随着每次遍历(从0开始)递增,当遍历到最后一个时,$last的值为true,所以可以通过判断$last的值来监听ng-repeat的执行状态,
怎么在遍历过程中拿到$last的值:自定义指令
调用时使用angular调用指令的方法就可以了。
Demo
在ng-repeat模板实例内部会暴露出一些特殊属性$index/$first/$middle/$last/$odd/$even,$index会随着每次遍历(从0开始)递增,当遍历到最后一个时,$last的值为true,所以可以通过判断$last的值来监听ng-repeat的执行状态,
怎么在遍历过程中拿到$last的值:自定义指令
var app = angular.module('app',[]); app.directive('repeatFinish',function(){ return { link: function(scope,element,attr){ console.log(scope.$index) if(scope.$last == true){ scope.$eval( attr.repeatFinish ); } } } }) app.controller('appCtrl',function($scope,$element){ $scope.arr = [1,2,3,4,5,6]; $scope.tip = ''; //定义repeat完成后要执行的方法,方法名可自行定义 $scope.repeatDone = function(){ $scope.tip = 'ng-repeat完成,我要开始搞事情了!!!'; //执行自己要执行的事件方法 } });
调用时使用angular调用指令的方法就可以了。
<div ng-repeat="i in arr" repeat-finish="repeatDone();"> <p ng-bind="i"></p> </div>
Demo
相关文章推荐
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- Angularjs通过指令监听ng-repeat渲染完成后执行脚本的方法
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- AngularJS监听ng-repeat渲染完成的两种方法
- AngularJS监听ng-repeat渲染完成的方法
- angularJs监听ng-repeat渲染完成
- angular ng-repeat中DOM的重新渲染机制(项目中遇到问题)
- AngularJS ng-repeat指令及Ajax的应用实例分析
- (四上)Angular指令,ng-bind指令,ng-init 指令,ng-click指令,ng-repeat 指令
- AngularJS中ng-repeat渲染完成事件和中间变量的引用
- AngularJS ng-repeat指令中使用track by子语句解决重复数据遍历错误问题
- 如何知道ng-repeat循环的dom已经渲染完成
- angular 获取ng-repeat完成状态 $last
- angularjs directive指令 link在渲染完成之后执行