angular 数据加载完毕执行js方法

自定义了一条指令:

//on-finish-render="ngRepeatFinished"  load js after render datas
UserManagerApp.directive('onFinishRender', function ($timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function () {
                    scope.$emit('ngRepeatFinished');
                });
            }
        }
    }
})

解释下: 
link中当scope中的$last(也就是最后一条数据),加载完毕后触发’ngRepeatFinished’。

scope.$emit(‘ngRepeatFinished’);这句话就相当于trigger,会触发定义的事件监听: 
也就是我们在controller中会添加:

$scope.$on('ngRepeatFinished', function( ngRepeatFinishedEvent ) {})
  • 1

注:记得在html中需要加载数据的位置增加指令属性哦!

<tr ng-repeat="item in userList" on-finish-render>

猜你喜欢

转载自blog.csdn.net/zhang__ao/article/details/80337457