Calling a function when ng-repeat has finished

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

    I'm very surprised not to see the most simple solution among the answers to this question. What you want to do is add an ngInit directive on your repeated element (the element with the ngRepeat directive) checking for $last (a special variable set in scope by ngRepeat which indicates that the repeated element is the last in the list). If $last is true, we're rendering the last element and we can call the function we want.

    ng-init="$last && test()"
    

    The complete code for your HTML markup would be:

    {{t}}

    You don't need any extra JS code in your app besides the scope function you want to call (in this case, test) since ngInit is provided by Angular.js. Just make sure to have your test function in the scope so that it can be accessed from the template:

    $scope.test = function test() {
        console.log("test executed");
    }
    

提交回复
热议问题