This is what we do in our projects.
- link contains exclusively code that need access to the DOM. We tried to
always keep the link as small as possible.
- controller contains all the business logic and code that doesnt depend in
the DOM. We have a lot of nested and/or composed directives. Arguments
needed to be provided to child directives are declared here. Other thing we
do is that every controller that becomes a little complex (there is an
exact number, but more than 10-15 lines) needs to be extracted to a
separate file. You can declare a
angular.module().controller('ControllerName') and in the directive config
object, in the controller property, you can provide the string name of a
controller, and angularjs will use that controller for that directive. We
enforce to do that because it is much better for testability. This pattern
enforce to keep directives as simple as possible. And trust me, we have a
bunch a veeery complex directives with really complex business logic,
multiple dependency injection, etc.
I have never use compile, never found a situation where I need it and I'm
very familiar with it.
On Tuesday, August 5, 2014 4:37:52 AM UTC+2, Eric Eslinger wrote:
>
> 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]
> <javascript:>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> 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.