Calling order of link function in nested and repeated angularjs directives

后端 未结 2 1324
时光取名叫无心
时光取名叫无心 2020-12-24 09:25

I\'m fairly new to Javascript programming and I have only touched upon AngularJS. In order to evaluate it I decided to write a simple note application. The model is really s

相关标签:
2条回答
  • 2020-12-24 10:20

    Quoting Josh D. Miller who had kindly responded to a similar question I had :

    " Just a couple of technical notes. Assume that you have this markup:

    <div directive1>
      <div directive2>
        <!-- ... -->
      </div>
    </div>
    

    Now AngularJS will create the directives by running directive functions in a certain order:

    directive1: compile

    directive2: compile

    directive1: controller

    directive1: pre-link

    directive2: controller

    directive2: pre-link

    directive2: post-link

    directive1: post-link

    By default a straight "link" function is a post-link, so your outer directive1's link function will not run until after the inner directive2's link function has ran. That's why we say that it's only safe to do DOM manipulation in the post-link. "

    0 讨论(0)
  • 2020-12-24 10:23

    On a single element the order of the linking functions is determined by the order of the Compile Functions which in turn is ordered according to the priority property of the directive definition object.

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

    On multiple elements with transclusion : the inner directives are evaluated before outer directives. Reason: Nature of transclusion.

    On multiple elements with siblings : Executed in order top to bottom. Reason: Parsing logic of $compile.

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