您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

ng-repeat完成后调用函数

ng-repeat完成后调用函数

var module = angular.module('testApp', [])
    .directive('onFinishRender', function ($timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function () {
                    scope.$emit(attr.onFinishRender);
                });
            }
        }
    }
});

请注意,我没有使用.ready(),而是将其包装在中$timeout$timeout确保在ng重复元素真正完成渲染后$timeout执行(因为将会在当前摘要周期的末尾执行-)。因此,ng- repeat完成后,我们$emit将向外部范围(同级和父范围)发出事件。

然后在您的控制器中,可以使用以下命令进行捕获$on

$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
    //you also get the actual event object
    //do stuff, execute functions -- whatever...
});

使用html看起来像这样:

<div ng-repeat="item in items" on-finish-render="ngRepeatFinished">
    <div>{{item.name}}}<div>
</div>
其他 2022/1/1 18:15:08 有556人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶