angular學習筆記(三十)-指令(6)-transclude()方法(又稱linker()方法)-模擬ng-repeat指令

angular學習筆記(三十)-指令(4)-transclude文章的末尾提到了,若是在指令中須要反覆使用被嵌套的那一坨,須要使用transclude()方法.css

angular學習筆記(三十)-指令(5)-link文章也提到了link函數的第五個參數linker.html

這篇文章就來說解一下transclude()方法(linker()方法),是怎麼使用的,另外,它也是compile函數的第三個參數,用法同樣.git

下面就經過本身寫一個簡易的模擬ngRepeat的指令cbRepeat,來了解linker函數是怎麼工做的.同時能夠增進對指令的理解.github

html:數組

<!DOCTYPE html>
<html ng-app="dirAppModule">
<head>
  <title>20.8.1 指令-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">
    <span ng-click="reset()">添加一個元素</span>
    <span ng-click="resetBunny()">修改一個元素</span>
    <div cb-repeat="thing in things">
      <my-widget><span>{{thing}}</span></my-widget>
    </div>
  </div>
</body>
</html>

js:app

/*20.8.1 指令-compile和link*/
var appModule = angular.module('dirAppModule',[]);
appModule.controller('compileCtrl',function($scope){
    $scope.things=['bunny','cat','dog'];
    $scope.reset=function(){
        $scope.things.push('pig')
    };
    $scope.resetBunny=function(){
        $scope.things[0]='Bunny'
    }
});
appModule.directive('cbRepeat',function(){
    return {
        restrict:'A',
        transclude:'element',
        compile:function(tEle,tAttrs,trans){
            console.log('compile-cbRepeat');
            return function(scope,iEle,iAttrs,ctrl,linker){
                console.log('post-cbRepeat');
                //scope.$new()建立一個做用域的子做用域
                //console.log(scope.$new().$parent==scope);
                var myLoop = iAttrs.cbRepeat,
                    match = myLoop.match(/\s*(.+)\s+in\s+(.*)\s*/),
                    indexString = match[1],
                    collectionString = match[2],
                    parentEle = iEle.parent(),
                    elements = [];
                scope.$watchCollection(collectionString,function(collection){
                    if(elements.length>0){
                        for(var i= 0;i<elements.length;i++){
                            elements[i].el.remove();
                            elements[i].scope.$destroy();
                        }
                        elements = [];
                    }
                    for(var i=0;i<scope[collectionString].length;i++){
                        var newScope = scope.$new();
                        newScope[indexString] = scope[collectionString][i];
                        linker(newScope,function(clone){
                            parentEle.append(clone);
                            var element = {};
                            element.el = clone;
                            element.scope = newScope;
                            element.scope.$on('$destroy',function(){
                                console.log('被移除')
                            });
                            elements.push(element);
                        })
                    }
                })
            }
        }
    }
});
appModule.directive('myWidget',function(){
    return {
        restrict:'E',
        templateUrl:'text.html',
        replace:true,
        transclude:true,
        scope:true,
        compile:function(tEle,tAttrs,trans){
            //compile函數的tEle是原始的templateElement,也就是<div><h3 ng-transclude></h3></div>
            console.log('compile-myWidget'+tEle.html());
            return function(scope,iEle,iAttrs){
                //link函數的iEle是tEle通過實例化之後的instanceElement,也就是
                //<div><h3 ng-transclude=""><span class="ng-binding ng-scope">{{thing}}</span></h3></div>
                console.log('post-myWidget'+iEle.html())
            }
        }
        //簡單的說,tElement就是原始的,元素一開始是什麼樣子,它仍是什麼樣子,因此它沒有做用域.
        //而iElement是通過ng編譯的,添加了ng-binding,ng-scope,因此它有做用域.
    }
});

顯示結果:dom

→點擊'添加一個元素':→點擊'修改一個元素':函數

下面來解釋這整個過程:oop

1.獲取指令元素的cb-repeat屬性.post

2.經過正則匹配出cb-repeat的值的in先後的內容. indexString 和 collectionString

3.獲取指令元素的父元素

4.新建一個element數組,用於存放被重複的元素,其中每一項都是一個對象,這個對象有兩個屬性:

  (1)el屬性,就是相應的dom元素

  (2)scope屬性,每個dom元素獨立的scope,在ng-repeat中,若是是'list in lists',那麼,每一個被repeat出來的元素,都有一個scope,而各個list則會被綁定到各個scope下.

5.使用$watchCollection監測數據模型的變化,$watchCollection監測的是數組的長度是否發生變化,僅當數組中的元素髮生增減時,纔會觸發回調.這裏只是個簡單的模擬,因此當數據模型發生增減變化時,更新cb-repeat視圖,實際中數組中每一項內容發生變化應該都要更新視圖...

6.首先檢查element數組是否爲空,若是不爲空,則須要先清空element數組,清空element數組分爲三步:

  (1).將數組中每一個對象的el屬性,也就是每一個dom元素,都從視圖中remove掉

  (2).將數組中每一個對象的scope屬性,也就是每一個dom元素對應的scope都destroy掉

  (3).清空數組中的全部對象.

7.循環數據模型.根據最新的數據模型建立對應的視圖,分爲如下幾個步驟:

  (1).爲循環出來的元素建立一個獨立的scope,這個scope必須繼承父scope: 

      代碼裏被註釋的有這麼兩句:    

      scope.$new()建立一個做用域的子做用域
      console.log(scope.$new().$parent==scope);       結果是true

      也就是說,每一個scope都有一個$new()方法,建立一個子做用域,子做用域繼承了父做用域.子做用域的$parent屬性能夠訪問到父做用域

  (2).newScope[indexString] = scope[collectionString][i];

      將數據模型數組裏的每一個對象分別綁定到各自的做用域下

  (3).經過link的第五個參數linker(同compile第三個參數),來建立repeat元素:

                        linker(newScope,function(clone){
                            parentEle.append(clone);
                            var element = {};
                            element.el = clone;
                            element.scope = newScope;
                            element.scope.$on('$destroy',function(){
                                console.log('被移除')
                            });
                            elements.push(element);
                        })

        linker函數有兩個參數,第一個參數是一個scope,第二個參數是一個函數,函數接受一個參數clone,clone的內容就至關於transclude裏面提到的被嵌套的那一坨內容(angular學習筆記(三十)-指令(4)-transclude).而且clone被封裝成jqLite對象.能夠調用jqLite的方法.最重要的是,clone這個元素的做用域就是第一個被傳入的參數scope.

        而後在函數中咱們操做clone.新建一個對象,它的el屬性就是clone元素.它的scope屬性就是clone的scope.而後給scope綁定$destroy事件.而後一一push到element數組裏去.

        *須要被注意的是,$destroy這個事件,名字上是銷燬做用域,意義上也是銷燬做用域.可是這個做用域它其實仍是存在的.它最主要的做用只是綁定自定義的事件,而後當銷燬做用域的時候觸發這個事件而且冒泡.好比這裏我定義的$destroy事件爲function(){console.log('被移除')},那麼,在上面elements[i].scope.$destroy()的時候,就會觸發這個回調.

 

另外,注意一下這兩個指令的compile和link的執行順序問題:

 

 

這個順序就是angular學習筆記(三十)-指令(7)-compile和link(3)這篇文章裏的第一種狀況.

注意myWidget因爲是複製出來的,因此它的compile只會執行一次,link仍是會執行屢次. 

 

這個cbRepeat只是一個簡單的模擬,和真正的ngRepeat仍是區別很大的.僅做了解嘗試.

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

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

相關文章
相關標籤/搜索