Calling a function when ng-repeat has finished

前端 未结 10 1798
面向向阳花
面向向阳花 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 03:18

    If you’re not averse to using double-dollar scope props and you’re writing a directive whose only content is a repeat, there is a pretty simple solution (assuming you only care about the initial render). In the link function:

    const dereg = scope.$watch('$$childTail.$last', last => {
        if (last) {
            dereg();
            // do yr stuff -- you may still need a $timeout here
        }
    });
    

    This is useful for cases where you have a directive that needs to do DOM manip based on the widths or heights of the members of a rendered list (which I think is the most likely reason one would ask this question), but it’s not as generic as the other solutions that have been proposed.

提交回复
热议问题