AngularJs(Part 11)--自訂Directive

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   使用   sp   

先對自訂Directive有一個大體的映像

myModule.directive(‘myDirective‘,function(injectables){    var directiveDefinitionObject={        restrict:string,        priority:number,        template:string,        templateUrl:string,        replace:bool,        transclude:bool,        scope:bool or object,        controller:function controllerConstructor($scope,$element,$attrs,$transclude){...},        require:string,        link:function postLink(scope,iElement,iAttrs){...},        compile:function compile(tElement,tAttrs,transclude){            ...            return {                pre:functionLink(scope,iElement,iAttrs,controller){...},                post:functionLink(scope,iElement,iAttrs,controller){...}            };        }    };});
View Code

 重點在directiveDefinitionObject這個對象。這個對象當中有些屬性是互斥的,即:有了A屬性,就不需要B屬性。但大部分是可選的

以下是詳細解釋各個屬性的作用
1. restrict: 決定directive是做HTML tag,屬性,類或者是注釋,只能在 ‘E‘,‘A‘,‘C‘,‘M‘或者這四個字母的任意組合中取值。參考下表
    值      例子
    E        <my-directive></my-directive>
    A        <div my-directive=‘abc‘></div>
    C        <div class=‘my-directive:abc‘></div>
    M        <!--this is abc my-directive:abc -->
    restrict預設是A,如果一個directive既想用在Tag中也想用在注釋中,那麼就用 ‘EM‘
2. priority 設定優先權,數字大的先運行。一個例子是ng-repeat ,必須要先跑ng-repeat,生產出重複的對象,再跑其他
3. template和templateUrl 模板或者模板地址。有時我們把一大段HTML片段封裝在一個AngularJS中,如我只想用一個<hello></hello>來表示<div>hello <span style=‘color:red;‘>world</span></div>那麼可以把template設定為這一段字串,或者寫道另一個檔案中用templateUrl引用。當然,還可以通過其他方法載入模板,然後把它們存在$templateCache中,如下。
   module.run(function($templateCache){
        $templateCache.put("a.html","<div>hello <span style=‘color:red;‘>world</span></div>");
   });
4. transclude 有時我們想保留原始的內容,並把它們添加到模板中,如有<hello>,we have fun</hello>。 想實現的效果是<div>hello world, we have fun</div>。如果我們設定了template為

<div>hello world</div>,那麼"we have fun"這一段不見了,現在設定transclude為true,並把template設定為<div>hello world<span ng-transclude></span></div>,就可以達成需要的效果了。注意template中多了個ng-transclude
5.compile和link
    AngularJS的directive要起作用必須經過兩個階段:
    1) compile階段,在該階段AngularJS搜尋template中的整個DOM樹,找到所有註冊的其他directive,把他們按照各自的templat、replace等設定轉換為DOM,運行各自的compile的function
    2) link階段,在該階段主要負責保持model和view的一致性,就是各種加監聽器。在這個階段scope在被注入到link中,所有的directive才有用
    
    ...
    
6.scope AngularJS提供了三種scope給directive
    1).就用DOM中已經存在的scope  (false)
    2).繼承自從已經存在的scope,可以訪問父級scope中的所有properties  (true)
    3).一個獨立的scope。不可以訪問父級scope的properties ,但他有一個$parent屬性指向父級scope  (一個{/*properties*/}對象)
    對於前兩種沒什麼好說的,對於第三種,看一個例子。
    現在想完成一個點擊之後顯示一些div的功能。HTML代碼如下:
    <div ng-controller=‘MyController‘>
        <expander expand-title=‘title‘ expand-text=‘text‘ expand-click=‘change()‘></expander>
    </div>
    javascript代碼如下:

angular.module(‘test‘,[])        .controller(‘MyController‘,[‘$scope‘,function($scope){                $scope.title=‘click me i am the title‘;                $scope.text=‘well ,i was hidden but now i am shown‘;                $scope.change=function(){                    $scope.text=‘haha i change myself‘;                };                            }])        .directive(‘expander‘,function(){            return {                restrict:‘E‘,                replace:true,                transclude:true,                template:[‘<div>‘,                            ‘<div id="d1" ng-click="toggle()">{{scopeTitle}}</div>‘,                            ‘<div id="d2" ng-show="showMe" ng-transclude>{{scopeText}}</div>‘,                            ‘<div id=‘d3‘ ng-click="scopeChange()">click me to change</div>‘,                          ‘</div>‘].join(‘‘),                scope:{scopeTitle:‘@expandTitle‘,                        scopeText:‘=expandText‘,                        scopeChange:‘&expandClick‘},                link:function(scope,element,attributes){                    scope.showMe=false;                    scope.toggle=function(){                        scope.showMe=!scope.showMe;                    };                }            };        })        ;
View Code

    結果是在d1中顯示的是title,因為scopeTitle取得是‘@expandTitle‘,意思是把expand-title的屬性值當作字串傳給scopeTitle;

    而d2中顯示的是“well ,i was hidden but now i am shown”,因為scopeText取得是‘=expandText‘,意思是把expand-text的值當作變數名,在父級scope中找到同名的變數,把這個變數的值傳給           scopeText;
    再看d3中的ng-click,它的值是通過scopeChange:‘&expandClick‘傳進來的,expand-click的值是父級scope中的scope方法。所以’&‘可以用來傳遞方法。

4.controller directives之間需要互相通訊是,可以通過controller來進行
    controller: function($scope,$element,$attrs,$transclude){}
    其他的directive可以接受這個controller:通過require屬性 require:"^?directiveName".
    directiveName:是駝峰標記法的directive的名字。需要哪個directive的controller就寫哪個的名字;
    ^:預設的AngularJS是找同一個元素內的directive,如果加上了“^”,AngularJS沿著DOM樹往上找,知道找到為止;
    ?:如果沒找到controller,AngularJS會丟出一個錯誤,加上了"?"表示沒找到也沒關係;
    重寫一個例子,要求出現三個tag,點擊其中一個的話,自己展開,別的收縮  

angular.module(‘test‘,[])        .directive(‘accordion‘,function(){            return {                restrict:"E",                replace:true,                transclude:true,                template:‘<div ng-transclude></div>‘,                controller:function(){                    var expanders=[];                    this.getOpened=function(selected){                        angular.forEach(expanders,function(expander){                            if(selected!=expander){                                expander.showMe=false;                            }                        });                    };                    this.addExpander=function(expander){                        expanders.push(expander);                    };                }            };        })        .directive(‘expander‘,function(){            return {                restrict:‘E‘,                replace:true,                transclude:true,                require:"^?accordion",                template:[‘<div>‘,                            ‘<div ng-click="toggle()">{{scopeTitle}}</div>‘,                            ‘<div ng-show="showMe" ng-transclude>{{scopeText}}</div>‘,                          ‘</div>‘].join(‘‘),                scope:{scopeTitle:‘=expandTitle‘,                        scopeText:‘=expandText‘,                      },                link:function(scope,element,attrs,accordionController){                    scope.showMe=false;                    accordionController.addExpander(scope);                    scope.toggle=function(){                        scope.showMe=!scope.showMe;                        accordionController.getOpened(scope);                    };                }            };        })        .controller(‘MyController‘,[‘$scope‘,function($scope){            $scope.expanders=[{                title:‘click1‘,                text:‘text1‘            },{                title:‘click2‘,                text:‘text2‘            },{                title:‘click3‘,                text:‘text3‘            }];        }]);
View Code

    再看HTML:
    <div ng-controller=‘MyController‘>
        <accordion>
            <expander ng-repeat=‘expander in expanders‘ expand-title=‘expander.title‘ expand-text=‘expander.text‘></expander>
        <accordion>
    </div>
    可以想見,我要實現一個展開就其他收縮的功能,我得有一個地方儲存所有expander的狀態。儘管在expander的link中的scope可以訪問到MyController的scope從而可以找到expanders,但是最好不要這樣做,還是隔離的好。那麼最好的地方就是在expander的父元素accordion中儲存。accordion相當於一個倉庫,他提供了API供別人使用。
   

AngularJs(Part 11)--自訂Directive

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.