Generally speaking with directives you can almost always use link. Which is actually the post-link function (there's a rarely used pre-link one too). Some of the other functions are used for timing purposes (child directives get linked either before or after parent directives, i can never remember, so you need to declare a controller on a parent directive if you want your children to have access to the outer directive controller - like tab directives in a tabset super-scope).
Compile functions (working from memory here, so maybe someone else with call me on this) get called once per page - if you do a <custom-directive tuber="potato" ng-repeat="potato in potatoes"> tag, the compile function gets called once, and generates a template that can be linked, and then the link function is called once per potato, each time with a different scope and a different scope.tuber. So: generally speaking, I use link functions in directives pretty exclusively, *unless* I need to pass scope stuff from enclosing to enclosed directives, in which case the parent directive needs a controller. I almost never use compile, *unless* I'm mucking around with fancy transclusion stuff, and then I'm almost certainly making a mistake, or overcomplicating things. e On Mon, Aug 4, 2014 at 7:12 AM, Darin Hensley <[email protected]> wrote: > When writing directives, I could easily use the link function and never > have a issue. I have read that the compile function is beneficial for large > amounts of data in the directive. I have also read that I should use the > link function for when I need to only modify scope members, and the compile > function when I modify the DOM. And then there leaves the controller > function(of course, not using a external controller outside of the > directive) in the directive. Again, I find that I can use the link function > in every situation and not have a issue.... > > But, I would like to learn and use the correct Angular.js convention when > writing directives and know if which to use for what is situation. Can > anyone please explain? Examples, would help a lot to if possible, please. > > > Thanks, > -Darin > > -- > You received this message because you are subscribed to the Google Groups > "AngularJS" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
