Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

post link vs pre link in Angular js directives

Tags:

angularjs

As outlined here:

http://docs.angularjs.org/guide/directive

Angular js directives take two different types of link functions:

Pre-linking function Executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking.

Post-linking function Executed after the child elements are linked. It is safe to do DOM transformation in the post-linking function.

Additionally, it appears that the default key of link will bind to postLink if given an anonymous function.

When and why would I ever want to use a pre link function?

like image 815
Abraham P Avatar asked Sep 09 '25 20:09

Abraham P


1 Answers

The only time you'd want to use a pre link is when you need to perform some preparation on the scope before any child elements compile.

My team has used it when writing a grid directive to define the grid object on the scope and setup some of its properties that are needed before any of the child row and cell objects are compiled.

Hope that helps!

like image 161
thechrisman Avatar answered Sep 14 '25 23:09

thechrisman