AngularJS Directives: Are Link and Compile functions meant to work together?

A couple of days ago a nice article has been published by Jurgen Van de Moere on "The nitty-gritty of compile and link functions inside AngularJS directives". It explains quite clearly on the responsibilities and sequence of the compile, pre-link and post-link functions.


(source: jvandemo.com)

You should definitely check it out: http://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives


link and compile do not work together, no.

In the directive definition object, if you only define link, that's like shorthand for having an empty compile function with an empty preLink function with your code in the postLink function. As soon as you define compile, link is ignored by angular, because compile should return the linking functions.

If you only return one function from compile, then it'll be executed post link.

Or, put differently, link is just a shortcut to the postLink function that gets called after the scope has been linked by compile.

It's (sort of) documented here - look at the comments in the code sample.