Calling a function when ng-repeat has finished

前端 未结 10 1797
面向向阳花
面向向阳花 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:59

    A solution for this problem with a filtered ngRepeat could have been with Mutation events, but they are deprecated (without immediate replacement).

    Then I thought of another easy one:

    app.directive('filtered',function($timeout) {
        return {
            restrict: 'A',link: function (scope,element,attr) {
                var elm = element[0]
                    ,nodePrototype = Node.prototype
                    ,timeout
                    ,slice = Array.prototype.slice
                ;
    
                elm.insertBefore = alt.bind(null,nodePrototype.insertBefore);
                elm.removeChild = alt.bind(null,nodePrototype.removeChild);
    
                function alt(fn){
                    fn.apply(elm,slice.call(arguments,1));
                    timeout&&$timeout.cancel(timeout);
                    timeout = $timeout(altDone);
                }
    
                function altDone(){
                    timeout = null;
                    console.log('Filtered! ...fire an event or something');
                }
            }
        };
    });
    

    This hooks into the Node.prototype methods of the parent element with a one-tick $timeout to watch for successive modifications.

    It works mostly correct but I did get some cases where the altDone would be called twice.

    Again... add this directive to the parent of the ngRepeat.

提交回复
热议问题