Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to expand your question a bit and also include the compile function.</p> <ul> <li><p><em>compile function</em> - use for <strong>template</strong> DOM manipulation (i.e., manipulation of tElement = template element), hence manipulations that apply to all DOM clones of the template associated with the directive. (If you also need a link function (or pre and post link functions), and you defined a compile function, the compile function must return the link function(s) because the <code>'link'</code> attribute is ignored if the <code>'compile'</code> attribute is defined.)</p></li> <li><p><em>link function</em> - normally use for registering listener callbacks (i.e., <code>$watch</code> expressions on the scope) as well as updating the DOM (i.e., manipulation of iElement = individual instance element). It is executed after the template has been cloned. E.g., inside an <code>&lt;li ng-repeat...&gt;</code>, the link function is executed after the <code>&lt;li&gt;</code> template (tElement) has been cloned (into an iElement) for that particular <code>&lt;li&gt;</code> element. A <code>$watch</code> allows a directive to be notified of scope property changes (a scope is associated with each instance), which allows the directive to render an updated instance value to the DOM.</p></li> <li><p><em>controller function</em> - must be used when another directive needs to interact with this directive. E.g., on the AngularJS home page, the pane directive needs to add itself to the scope maintained by the tabs directive, hence the tabs directive needs to define a controller method (think API) that the pane directive can access/call. <br> <br>For a more in-depth explanation of the tabs and pane directives, and why the tabs directive creates a function on its controller using <code>this</code> (rather than on <code>$scope</code>), please see <a href="https://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers/14168699#14168699">&#39;this&#39; vs $scope in AngularJS controllers</a>.</p></li> </ul> <p>In general, you can put methods, <code>$watches</code>, etc. into either the directive's controller or link function. The controller will run first, which sometimes matters (see this <a href="http://jsfiddle.net/mrajcok/xfh3F/" rel="noreferrer">fiddle</a> which logs when the ctrl and link functions run with two nested directives). As Josh mentioned in a <a href="https://stackoverflow.com/questions/14620521/how-to-write-a-directive-in-angularjs/14621193#comment20441740_14621193">comment</a>, you may want to put scope-manipulation functions inside a controller just for consistency with the rest of the framework.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload