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


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

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

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

下面就通過自己寫一個簡易的模擬ngRepeat的指令cbRepeat,來了解linker函數是怎么工作的.同時可以增進對指令的理解.

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:

/*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,所以它有作用域.
    }
});

顯示結果:

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

下面來解釋這整個過程:

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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM