摘要
directive(指令)是angular的一個(gè)非常強(qiáng)大又有用的功能,它相當(dāng)于實(shí)現(xiàn)了組件化的概念。我們可以通過它公共地自定義DOM元素或CLASS類或ATTR屬性,并且在這基礎(chǔ)上進(jìn)行操作scope、綁定事件等等。將一些公共的模塊或操作封裝到指令中,然后就可以在html頁面中編寫簡(jiǎn)單的一行代碼就可以加載整個(gè)公共模塊,大大避免了代碼的冗余。一般使用directive有以下場(chǎng)景:
下面我想通過一些實(shí)例結(jié)合分析對(duì)我所了解的directive進(jìn)行一些簡(jiǎn)單的歸納總結(jié)(我所使用的是angular1.5.3):
一、Directive的使用
angular.module("app",[]).directive("directiveName",function(){return{//通過設(shè)置項(xiàng)來定義};})二、一個(gè)簡(jiǎn)單的實(shí)例
html代碼:
<!DOCTYPE html><html ng-app='helloApp'> <body> <hello></hello> </body> <script src="js/angular.min.js"></script> <script src="js/helloDirective.js"></script></html>
js代碼:
var appModule = angular.module('helloApp', []);appModule.directive('hello', function() { return { restrict: 'E', template: '<div>Hello,friends!</div>', replace: true };});效果截圖:

實(shí)例解析:
1、restrict:EACM的子集的字符串,它限制directive為指定的聲明方式。
2、默認(rèn)情況下,directive將僅僅允許通過屬性聲明,ECA較常用。
template:指令顯示的模板內(nèi)容,一般由html標(biāo)簽和文本組成。通常情況下html內(nèi)容較簡(jiǎn)單的情況下使用,模板內(nèi)容復(fù)雜的建議將公共部分抽離到html文件中,使用templateUrl屬性。
templateUrl - 與template基本一致,但模版通過指定的url進(jìn)行加載。因?yàn)槟0婕虞d是異步的,所以compilation、linking都會(huì)暫停,等待加載完畢后再執(zhí)行。
3、replace:如果設(shè)置為true,那么模版將會(huì)替換當(dāng)前元素,而不是作為子元素添加到當(dāng)前元素中。(注:為true時(shí),模版必須有一個(gè)根節(jié)點(diǎn))
上述實(shí)例dom節(jié)點(diǎn)截圖:

三、實(shí)例2:關(guān)于transclude
修改上面實(shí)例代碼:
<!DOCTYPE html><html ng-app='helloApp' ng-controller="myController"> <body> <hello>{{myName}}</hello> </body> <script src="js/angular.min.js"></script> <script src="js/helloDirective.js"></script></html>var appModule = angular.module('helloApp', []);appModule.directive('hello', function() { return { restrict: 'E', template: '<div>Hello,my name is <span ng-transclude></span></div>', replace: true, transclude:true };});效果截圖:


解析:
transclude:指令的作用是把我們自定義的語義化標(biāo)簽替換成瀏覽器能夠認(rèn)識(shí)的HTML標(biāo)簽。上述例子replace設(shè)置為true,模版將會(huì)替換當(dāng)前元素。而transclude設(shè)置為true的作用可以簡(jiǎn)化地理解成:把<hello>標(biāo)簽替換成我們所編寫的HTML模板,但是<hello>標(biāo)簽內(nèi)部的內(nèi)容保持不變。而<span ng-transclude></span>則是指明標(biāo)簽內(nèi)部?jī)?nèi)容插入到模板內(nèi)容的哪個(gè)位置。
四、實(shí)例3:關(guān)于compile,link和controller
實(shí)例代碼:
phonecatDirectives.directive('exampleDirective', function() { return { restrict: 'E', template: '<p>Hello {{number}}!</p>', controller: function($scope, $element){ $scope.number = $scope.number + "22222 "; }, link: function(scope, el, attr) { scope.number = scope.number + "33333 "; }, compile: function(element, attributes) { return { pre: function preLink(scope, element, attributes) { scope.number = scope.number + "44444 "; }, post: function postLink(scope, element, attributes) { scope.number = scope.number + "55555 "; } }; } } }); //controller.js添加 dtControllers.controller('directive2',['$scope', function($scope) { $scope.number = '1111'; } ]); //html <body ng-app="phonecatApp"> <div ng-controller="directive2"> <example-directive></example-directive> </div> </body>運(yùn)行結(jié)果:
Hello 1111 22222 44444 55555!
由結(jié)果可以看出來,controller先運(yùn)行,compile后運(yùn)行,link不運(yùn)行。
將上例中的compile注釋掉的運(yùn)行結(jié)果:
Hello 1111 22222 33333!
由結(jié)果可以看出來,controller先運(yùn)行,link后運(yùn)行,link和compile不兼容。
簡(jiǎn)單解析:
指令的控制器和link函數(shù)(后面會(huì)講)可以進(jìn)行互換。區(qū)別在于,控制器主要是用來提供可在指令間復(fù)用的行為但link鏈接函數(shù)只能在當(dāng)前內(nèi)部指令中定義行為,且無法再指令間復(fù)用。
更多了解可以參考Angular官方站點(diǎn):https://angularjs.org/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注