小编Yat*_*era的帖子

为什么ng-repeat会改变链接函数执行的顺序

在嵌套指令上执行编译和链接函数的通常顺序如下

标记

<dir1>
  <div dir2="">
  </div>
</dir1>
Run Code Online (Sandbox Code Playgroud)

执行顺序

1) compile of directive 1
2) compile of directive 2
3) link of directive 2
4) link of directive 1
Run Code Online (Sandbox Code Playgroud)

假设dir1已将restrict属性设置为'E'dir2restrict设置为'A'

现在,如果ng-repeat在同一标记中使用指令,则执行顺序会发生变化

标记

<dir1>
  <div ng-repeat="item in items">
    <div dir2="">
    </div>
  </div>
</dir1>
Run Code Online (Sandbox Code Playgroud)

假设items是在范围上定义的,执行顺序变为

1) compile of directive 1
2) link of directive 1
3) compile of directive 2
4) link of directive 2
Run Code Online (Sandbox Code Playgroud)

Plunker - https://plnkr.co/edit/fRGHS1Bqu3rrY5NW2d97?p=preview …

angularjs angularjs-directive angularjs-ng-repeat angularjs-compile

5
推荐指数
1
解决办法
569
查看次数