wonderfan / javascript

Explore the power of HTML,CSS and JavaScript
1 stars 0 forks source link

The compile and link in the AngularJS #20

Closed wonderfan closed 9 years ago

wonderfan commented 9 years ago
  1. Step into a DOM node.
  2. Loop through registered directives to see if this node contains any.
  3. For each directive found:
    1. Determine if the current scope is to be used, or if a child or isolated scope needs to be created.
    2. Create a function that, when called, will execute the directive's linking function against the appropriate scope from step 3.1.
    3. Append that function to a list of functions to be executed later.
  4. Does the current DOM node have children?
    1. YES: Step into each one and go to step 1 above.
    2. NO: That's the end of this branch.
  5. Return a function that will execute all functions created in Step 3.2. This is your compiled view.
wonderfan commented 9 years ago

Here is one link to explain the compile, pre-link and post-link for directives http://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/

wonderfan commented 9 years ago

transclude: 转置

wonderfan commented 9 years ago

Good tutorials: http://triangular.io/blog/directive-compilation-in-angularjs-step-by-step/

wonderfan commented 9 years ago

Good tutorials: http://www.sitepoint.com/practical-guide-angularjs-directives/

wonderfan commented 9 years ago
                var directive = $injector.invoke(directiveFactory);
                if (isFunction(directive)) {
                  directive = { compile: valueFn(directive) };
                } else if (!directive.compile && directive.link) {
                  directive.compile = valueFn(directive.link);
                }