angular - ng-if - how to callback after ng-if template has been rendered

前端 未结 4 1229
再見小時候
再見小時候 2021-02-18 18:47

in Angular, I need to call a function after an element with certain class has been loaded. Display of the element is controlled via ng-if=\'expr\'. Value of $scope.expr is set a

4条回答
  •  一整个雨季
    2021-02-18 19:18

    Problem was that there is no way to know if angular is done with the digest loop, ensuring that all elements have been rendered.

    To solve this issue, there were two options

    1. Wrap your callback function in a directive. include that directive in ng-if. and make that expression true only when u want your callback to be executed.
    2. Call your callback function inside, setTimeOut(function(){ ... }, 0); as browsers by default keep all events in a queue, therefore, when digest loop is running, your callback function will enter the queue and get executed as soon digest loop is over.

提交回复
热议问题