is there a post render callback for Angular JS directive?
Solution 1:
If the second parameter, "delay" is not provided, the default behaviour is to execute the function after the DOM has completed rendering. So instead of setTimeout, use $timeout:
$timeout(function () {
//DOM has finished rendering
});
Solution 2:
I had the same problem and I believe the answer really is no. See Miško's comment and some discussion in the group.
Angular can track that all of the function calls it makes to manipulate the DOM are complete, but since those functions could trigger async logic that's still updating the DOM after they return, Angular couldn't be expected to know about it. Any callback Angular gives might work sometimes, but wouldn't be safe to rely on.
We solved this heuristically with a setTimeout, as you did.
(Please keep in mind that not everyone agrees with me - you should read the comments on the links above and see what you think.)
Solution 3:
You can use the 'link' function, also known as postLink, which runs after the template is put in.
app.directive('myDirective', function() {
return {
link: function(scope, elm, attrs) { /*I run after template is put in */ },
template: '<b>Hello</b>'
}
});
Give this a read if you plan on making directives, it's a big help: http://docs.angularjs.org/guide/directive