Calling a function when ng-repeat has finished

前端 未结 10 1654
面向向阳花
面向向阳花 2020-11-22 02:28

What I am trying to implement is basically a \"on ng repeat finished rendering\" handler. I am able to detect when it is done but I can\'t figure out how to trigger a functi

10条回答
  •  你的背包
    2020-11-22 02:58

    If you need to call different functions for different ng-repeats on the same controller you can try something like this:

    The directive:

    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.broadcasteventname ? attr.broadcasteventname : 'ngRepeatFinished');
                });
                }
            }
        }
    });
    

    In your controller, catch events with $on:

    $scope.$on('ngRepeatBroadcast1', function(ngRepeatFinishedEvent) {
    // Do something
    });
    
    $scope.$on('ngRepeatBroadcast2', function(ngRepeatFinishedEvent) {
    // Do something
    });
    

    In your template with multiple ng-repeat

    {{item.name}}}
    {{item.name}}}

提交回复
热议问题