26
votes

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?

1

1 Answers

21
votes

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!