【AngularJS】—— 10 指令的複用

來源:互聯網
上載者:User

標籤:

前面練習了如何自訂指令,這裡練習一下指令在不同的控制器中如何複用。

  —— 來自《慕課網 指令3》

  首先看一下一個小例子,通過自訂指令,捕獲滑鼠事件,並觸發控制器中的方法。

  單個控制器的標籤指令

  依然是先建立一個模組

var myAppModule = angular.module("myApp",[]);

  在模組的基礎上,建立控制器和指令

            myAppModule.controller("myAppCtrl",["$scope",function($scope){                $scope.count = 0;                $scope.loadData = function(){                    $scope.count = $scope.count+1;                    console.log("myAppCtrl load data!"+$scope.count);                }            }]);            myAppModule.directive("loader",function(){                return{                    restrict:"AE",                    transclude:true,                    template:"<div><div ng-transclude></div></div>",                    link:function(scope,element,attr){                        element.bind("mouseenter",function(){                            // scope.loadData();                            scope.$apply("loadData()");                        });                    }                }            });

  首先看一下建立的控制器,在其中建立了一個loadData方法,用於相應觸發事件,為了便於觀察結果,添加了一個計數器。

  下面的指令採用了屬性和標籤元素的使用方式:“AE”,為了得到效果,建立了一個內嵌的模板(避免沒有內容時,點擊不到)。

  並在link屬性的方法內,添加相應事件,方法中有三個參數:

  1 scope,範圍,用於調用相應的範圍的方法。

  2 element,指代建立的標籤

  3 attr,用於擴充屬性,稍後展示使用方法

  有了以上的準備工作,就可以在body裡面使用標籤了:

<div ng-controller="myAppCtrl">            <loader howToLoad="loadData()">第一個loader!</loader>        </div>

  

  如何複用指令

  以上僅僅是單個控制器的指令使用,一個指令在一個頁面中可以被多次使用,也就意味著,會有多個控制器使用該指令。

  那麼指令如何知道調用控制器的那個方法呢?這就用到了attr屬性。

  在建立指令時,調用attr擷取屬性的值

myAppModule.directive("loader",function(){                return{                    restrict:"AE",                    transclude:true,                    template:"<div><div ng-transclude></div></div>",                    link:function(scope,element,attr){                        element.bind("mouseenter",function(){                            // scope.loadData();                            // scope.$apply("loadData()");                            scope.$apply(attr.howtoload);                        });                    }                }            });

  就可以在body中按照如下的方式使用了:

        <div ng-controller="myAppCtrl">            <loader howToLoad="loadData()">第一個loader!</loader>        </div>        <div ng-controller="myAppCtrl2">            <loader howToLoad="loadData2()">第二個loader!</loader>        </div>

  需要注意的是:

  1 標籤中屬性使用駝峰法命名,在指令中要轉換成全部小寫。

  2 指令中調用的僅僅是屬性的名字,沒有方法括弧。

  3 應用時,屬性對應的值是該控制器內聲明的執行方法。

  

  下面看一下範例代碼:
<!doctype html><html ng-app="myApp">    <head>         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>    </head>    <body>        <div ng-controller="myAppCtrl">            <loader howToLoad="loadData()">第一個loader!</loader>        </div>        <div ng-controller="myAppCtrl2">            <loader howToLoad="loadData2()">第二個loader!</loader>        </div>        <script type="text/javascript">            var myAppModule = angular.module("myApp",[]);            myAppModule.controller("myAppCtrl",["$scope",function($scope){                $scope.count = 0;                $scope.loadData = function(){                    $scope.count = $scope.count+1;                    console.log("myAppCtrl load data!"+$scope.count);                }            }]);            myAppModule.controller("myAppCtrl2",["$scope",function($scope){                $scope.count = 0;                $scope.loadData2 = function(){                    $scope.count = $scope.count+1;                    console.log("myAppCtrl2 load data!"+$scope.count);                }            }]);            myAppModule.directive("loader",function(){                return{                    restrict:"AE",                    transclude:true,                    template:"<div><div ng-transclude></div></div>",                    link:function(scope,element,attr){                        element.bind("mouseenter",function(){                            // scope.loadData();                            // scope.$apply("loadData()");                            scope.$apply(attr.howtoload);                        });                    }                }            });        </script>    </body></html>

  實現的結果:

【AngularJS】—— 10 指令的複用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.