Directive - Post Link Function

28 views
Skip to first unread message

Chaitanya Babar

unread,
Jan 3, 2018, 6:57:53 AM1/3/18
to Angular and AngularJS discussion
Hi All,

I have used directive based approach in an AngularJS application . I have one parent container in which all the directives are displayed depending upon certain conditions.
On the parent container there is one directive of type attribute i.e. my-directive , which is used for traversing the whole of the DOM. i.e. adding a certain class to the child elements.
Now what is happening when initially page loads some of the child directives inside the parent container are still to be compiled , up till then my-directive tarverse all the way thriugh DOM & add class to
children as a result some child directives are missed of traversing . I have already placed the traverse() function inside the post link of my-directive (i.e considering the post link works bottom up) , 
but still traverse() function executes even if few directives are yet to be compiled. For time being i have called traverse() inside $timeout with some delay. I don't think using delay is proper way as some times it may break if some directives takes more time. So can you all please suggest what should be done in this case.

Thanks

Sander Elias

unread,
Jan 6, 2018, 5:55:11 AM1/6/18
to Angular and AngularJS discussion
Hi Chaitanya,

Assuming you are on an up to date version of AngularJS you should switch to using the $postlink life-cycle hook. Once that hook runs, you can be sure all child directives have been compiled and linked. That works only top-down. I'm not aware of anything that works bottom-up.

Regards
Sander

Chaitanya

unread,
Jan 14, 2018, 12:45:10 PM1/14/18
to Angular and AngularJS discussion
Hi Sander Elias,

First of all thanks for your suggestion.
With respect to angular documentation for $postlink ,says that it executes after the child directives are compiled ,but not when the child directives are using templateURL
What i need is any work around approach / event in angularjs that can tell us that DOM of child directives is loaded along with its bindings too.

Thanks,
Chaitanya
Reply all
Reply to author
Forward
0 new messages