I'm trying to write my own AngularJS directives. I think I understand the difference between the compile
and link
functions. This video clears it up nicely.
But I'm not sure now that I understand the role a controller plays in a directive. A directive can include a controller
function, and you can inject $element
, $attrs
and such into it, so it can do pretty much everything that the link
function can do and, I believe, the other way around.
So I have two questions:
- Are there important technical differences between the
controller
function and thelink
function? - What is best practice? What kind of work should I be doing in which?