Calling a function when ng-repeat has finished

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

    Use $evalAsync if you want your callback (i.e., test()) to be executed after the DOM is constructed, but before the browser renders. This will prevent flicker -- ref.

    if (scope.$last) {
       scope.$evalAsync(attr.onFinishRender);
    }
    

    Fiddle.

    If you really want to call your callback after rendering, use $timeout:

    if (scope.$last) {
       $timeout(function() { 
          scope.$eval(attr.onFinishRender);
       });
    }
    

    I prefer $eval instead of an event. With an event, we need to know the name of the event and add code to our controller for that event. With $eval, there is less coupling between the controller and the directive.

提交回复
热议问题