Thursday, February 26, 2015

About AngularJS directive

1. How directives are compiled:


HTML compilation happens in three phases:
  1. $compile traverses the DOM and matches directives.
    If the compiler finds that an element matches a directive, then the directive is added to the list of directives that match the DOM element. A single element may match multiple directives.
  2. Once all directives matching a DOM element have been identified, the compiler sorts the directives by their priority.
    Each directive's compile functions are executed. Each compile function has a chance to modify the DOM. Each compile function returns a link function. These functions are composed into a "combined" link function, which invokes each directive's returned link function.
  3. $compile links the template with the scope by calling the combined linking function from the previous step. This in turn will call the linking function of the individual directives, registering listeners on the elements and setting up $watchs with the scope as each directive is configured to do.

2. Difference between Controller and Link
    When do I want my code to run
  • Before compilation? – Controller
  • After compilation? – Link
3. Controller "$scope" and link "scope"
- controller ‘$scope’ and link ‘scope’ are the same thing. The difference is paramaters sent to the controller get there through Dependency Injection (so calling it ‘$scope’ is required), where parameters sent to link are standard order based funcitons.  

4. Why do we have "controller" in directive
So we can do awesome stuff like using one directives controller in another. We simply set something we want to “this” and get it using the “require” option in the directive.




Reference Link:

No comments:

Post a Comment