I want to call some jQuery function targeting div with table. That table is populated with ng-repeat
.
When I call it on
$(document).ready()
I have no result.
Also
$scope.$on('$viewContentLoaded', myFunc);
doesn't help.
Is there any way to execute function right after ng-repeat population completes? I've read an advice about using custom directive
, but I have no clue how to use it with ng-repeat and my div...
If you simply want to execute some code at the end of the loop, here's a slightly simpler variation that doesn't require extra event handling:
See a live demo.
It may also be necessary when you check the
scope.$last
variable to wrap your trigger with asetTimeout(someFn, 0)
. AsetTimeout 0
is an accepted technique in javascript and it was imperative for mydirective
to run correctly.i would like to add another answer, since the preceding answers takes it that the code needed to run after the ngRepeat is done is an angular code, which in that case all answers above give a great and simple solution, some more generic than others, and in case its important the digest life cycle stage you can take a look at Ben Nadel's blog about it, with the exception of using $parse instead of $eval.
but in my experience, as the OP states, its usually running some JQuery plugins or methods on the finnaly compiled DOM, which in that case i found that the most simple solution is to create a directive with a setTimeout, since the setTimeout function gets pushed to the end of the queue of the browser, its always right after everything is done in angular, usually ngReapet which continues after its parents postLinking function
for whoever wondering that in that case why not to use $timeout, its that it causes another digest cycle that is completely unnecessary
Here is a repeat-done directive that calls a specified function when true. I have found that the called function must use $timeout with interval=0 before doing DOM manipulation, such as initializing tooltips on the rendered elements. jsFiddle: http://jsfiddle.net/tQw6w/
In $scope.layoutDone, try commenting out the $timeout line and uncommenting the "NOT CORRECT!" line to see the difference in the tooltips.
JS:
Complementing Pavel's answer, something more readable and easily understandable would be:
Why else do you think
angular.noop
is there in the first place...?Advantages:
You don't have to write a directive for this...
I found an answer here well practiced, but it was still necessary to add a delay
Create the following directive:
Add it to your repeater as an attribute, like this:
According to Radu,:
For me it works, but I still need to add a setTimeout