AngularJS : How does the HTML compiler arrange the order for compiling?

前端 未结 3 1841
孤城傲影
孤城傲影 2021-01-13 22:14

When there are multiple directives across multiple elements on a page, how does Angular\'s HTML compiler arrange the order for compiling?

Say I have the following ma

相关标签:
3条回答
  • 2021-01-13 22:32

    The order of the linking and compiling functions is well described in the other answers and in the Angular documentation of the $compile function at https://docs.angularjs.org/api/ng/service/$compile:

    1. Compile and pre-link top-down (First parent, then children)
    2. Post-link the other way around
    3. Directives on the same element are treated by their priority attribute

    In addition: 4. When using the templateUrl option in a directive, that directive will not compile until the template is loaded, but the compiler will continue compiling other directives. This will break the compiling/linking order described above.

    0 讨论(0)
  • 2021-01-13 22:36

    I asked the same question in the AngularJS mailing list and Peter Bacon Darwin gave a great answer with a jsfiddle for demonstration. Link

    0 讨论(0)
  • 2021-01-13 22:41

    Regarding multiple directives on one element:

    This is handled using the directive 'priority' property. From the docs: Once all directives for a given DOM element have been identified they are sorted by priority and their compile() functions are executed.

    See http://docs.angularjs.org/guide/directive

    Regarding directive compile order:

    Angular will traverse the DOM - i.e. pick up the elements in the order they appear in the DOM tree. From the docs: Compile: traverse the DOM and collect all of the directives. The result is a linking function.

    See http://docs.angularjs.org/guide/compiler

    0 讨论(0)
提交回复
热议问题