angular學習筆記(三十)-指令(7)-compile和link(3)

詩&遠方發表於2014-09-19

本篇接著上一篇來講解當指令中帶有template(templateUrl)時,compile和link的執行順序:

把上一個例子的程式碼再進行一些修改:

1.將level-two指令改成具有templateUrl的指令,利用transclude,把level-three指令給巢狀到模板裡.(注意,level-two必須是一個擁有transclude屬性的指令,否則它的子元素裡的指令就被替換了,也就無所謂執行順序,失去討論意義)

html:

<!DOCTYPE html>
<html ng-app="dirAppModule">
<head>
  <title>20.8.2 指令-link和compile</title>
  <meta charset="utf-8">
  <script src="../angular.min.js"></script>
  <script type="text/ng-template" id="text.html">
    <div>
      <h3 ng-transclude></h3>
    </div>
  </script>
  <script src="script.js"></script>

  <style type="text/css">
    h3 {
      color:#CB2027
    }
  </style>
</head>
<body>
  <div ng-controller="compileCtrl">
    <level-one>
      <level-two>
        <level-three>
          hello,{{name}}
        </level-three>
      </level-two>
    </level-one>
  </div>
</body>
</html>

js:

/*20.8.2 指令-compile和link*/
var appModule = angular.module('dirAppModule',[]);
appModule.directive('levelOne',function(){
    return {
        restrict:'E',
        scope:true,
        compile:function(tEle,tAttrs,trans){
            console.log('compile→'+'levelOne'+tEle.html());
            return {
                pre:function(scope,iEle,iAttrs){
                    console.log('pre→'+'levelOne'+iEle.html())
                },
                post:function(scope,iEle,iAttrs){
                    console.log('post→'+'levelOne'+iEle.html())
                }
            }
        }
    }
});
appModule.directive('levelTwo',function(){
    return {
        restrict:'E',
        scope:true,
        templateUrl:'text.html',
        transclude:true,
        compile:function(tEle,tAttrs,trans){
            console.log('compile→'+'levelTwo'+tEle.html());
            return {
                pre:function(scope,iEle,iAttrs){
                    console.log('pre→'+'levelTwo'+iEle.html())
                },
                post:function(scope,iEle,iAttrs){
                    console.log('post→'+'levelTwo'+iEle.html())
                }
            }
        }
    }
});
appModule.directive('levelThree',function(){
    return {
        restrict:'E',
        scope:true,
        compile:function(tEle,tAttrs,trans){
            console.log('compile→'+'levelThree'+tEle.html());
            return {
                pre:function(scope,iEle,iAttrs){
                    console.log('pre→'+'levelThree'+iEle.html())
                },
                post:function(scope,iEle,iAttrs){
                    console.log('post→'+'levelThree'+iEle.html())
                }
            }
        }
    }
});

我們把level-two改成一個具有templateUrl的指令,並且使用transclude把level-three指令給巢狀到模板去.得到的結果如下:

下圖對比了一下當level-two指令擁有了template屬性後,compile和link的執行順序的改變:

所以可以總結如下步驟:

1.當遇到具有template屬性的指令,它的compile函式會被延後執行.

2.然後繼續向下尋找子元素的compile函式並執行.

3.然後從外向內執行pre-link,直到遇到具有template屬性的指令元素停止(這個例子裡就是level-two,所以就只執行了一個level-one的pre-link)

4.'3'步驟中的執行了pre-link的指令,從內向外反向執行post-link(這個例子只有一個level-one的post-link)

5.開始執行具有template屬性,被延遲compile函式.

6.按照正常順序,從具有template屬性的指令元素開始,從外向內執行pre-link函式.

7.'6'步驟中執行了pre-link的指令,從內向外反向執行post-link

8.很重要的一點:一個具有template屬性的指令元素,在compile階段,它的tElement已經是template裡面的原始內容了.在pre-link階段,它的iElement也是template裡面的原始內容,直到post-link階段,它的iElement才是經過編譯的template裡面的內容,比如這裡的ng-transclude裡面的內容,就是在post-link階段才有的.這和之前的正常順序執行的時候也是不同的.

關於這種情況,可以看下這篇文章的例子:

http://www.cnblogs.com/liulangmao/p/3980256.html

 

我們再修改一下這段程式碼:

2.將level-two指令改成具有templateUrl的指令,但是level-three不使用transclude巢狀到模板中,而是直接在模板中使用level-three指令:

   我們把level-two下的level-three放到template模板裡.然後再執行這段程式碼,結果如下:

<!DOCTYPE html>
<html ng-app="dirAppModule">
<head>
  <title>20.8.2 指令-link和compile</title>
  <meta charset="utf-8">
  <script src="../angular.min.js"></script>
  <script type="text/ng-template" id="text.html">
    <div>
      <h3>
        <level-three>
          hello,{{name}}
        </level-three>
      </h3>
    </div>
  </script>
  <script src="script.js"></script>

  <style type="text/css">
    h3 {
      color:#CB2027
    }
  </style>
</head>
<body>
  <div ng-controller="compileCtrl">
    <level-one>
      <level-two>
      </level-two>
    </level-one>
  </div>
</body>
</html>

 

我們把修改後的過程和修改前的過程進行對比:

總結如下:

1.由於level-two元素沒有子元素,所以compile執行到level-one就停止了.

2.在level-two的compile被執行前,它外層元素的link函式還是都會被先執行完.

3.然後按照正常順序執行下去就行了...

 

關於compile和link還有很多值得深究的問題.但是目前暫時就探索到這裡.compile和link的執行順序會影響到指令的編寫.(雖然大多數指令只要寫link屬性就行了).如果將來在angular的學習路上遇到更難的情況,再另外探索寫文.

完整程式碼:https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/20.8.2%20%E6%8C%87%E4%BB%A4.html

            https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/script.js

 

相關文章