angular directive的簡(jiǎn)單使用總結(jié)
摘要
directive(指令)是angular的一個(gè)非常強(qiáng)大又有用的功能,它相當(dāng)于實(shí)現(xiàn)了組件化的概念。我們可以通過(guò)它公共地自定義DOM元素或CLASS類或ATTR屬性,并且在這基礎(chǔ)上進(jìn)行操作scope、綁定事件等等。將一些公共的模塊或操作封裝到指令中,然后就可以在html頁(yè)面中編寫簡(jiǎn)單的一行代碼就可以加載整個(gè)公共模塊,大大避免了代碼的冗余。一般使用directive有以下場(chǎng)景:
- 使html更具有語(yǔ)義化,不需要深入研究和了解邏輯即可知道頁(yè)面的大致邏輯;
- 抽象出一個(gè)自定義的組件,以便在其他地方可以進(jìn)行復(fù)用。
下面我想通過(guò)一些實(shí)例結(jié)合分析對(duì)我所了解的directive進(jìn)行一些簡(jiǎn)單的歸納總結(jié)(我所使用的是angular1.5.3):
一、Directive的使用
angular.module("app",[]).directive("directiveName",function(){
return{
//通過(guò)設(shè)置項(xiàng)來(lái)定義
};
})
二、一個(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為指定的聲明方式。
- E - 元素名稱: <my-directive></my-directive>
- A - 屬性名:<div my-directive=”exp”></div>
- C - class名: <div class=”my-directive:exp;”></div>
- M - 注釋 : <!-- directive: my-directive exp -->
2、默認(rèn)情況下,directive將僅僅允許通過(guò)屬性聲明,ECA較常用。
template:指令顯示的模板內(nèi)容,一般由html標(biāo)簽和文本組成。通常情況下html內(nèi)容較簡(jiǎn)單的情況下使用,模板內(nèi)容復(fù)雜的建議將公共部分抽離到html文件中,使用templateUrl屬性。
templateUrl - 與template基本一致,但模版通過(guò)指定的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:指令的作用是把我們自定義的語(yǔ)義化標(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é)果可以看出來(lái),controller先運(yùn)行,compile后運(yùn)行,link不運(yùn)行。
將上例中的compile注釋掉的運(yùn)行結(jié)果:
Hello 1111 22222 33333!
由結(jié)果可以看出來(lái),controller先運(yùn)行,link后運(yùn)行,link和compile不兼容。
簡(jiǎn)單解析:
指令的控制器和link函數(shù)(后面會(huì)講)可以進(jìn)行互換。區(qū)別在于,控制器主要是用來(lái)提供可在指令間復(fù)用的行為但link鏈接函數(shù)只能在當(dāng)前內(nèi)部指令中定義行為,且無(wú)法再指令間復(fù)用。
更多了解可以參考Angular官方站點(diǎn):https://angularjs.org/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular ng-repeat指令實(shí)例以及擴(kuò)展部分
這篇文章主要為大家詳細(xì)介紹了Angular ng-repeat指令實(shí)例以及擴(kuò)展部分,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器
任何時(shí)候,如果我們想要為請(qǐng)求添加全局功能,例如身份認(rèn)證、錯(cuò)誤處理等,在請(qǐng)求發(fā)送給服務(wù)器之前或服務(wù)器返回時(shí)對(duì)其進(jìn)行攔截,是比較好的實(shí)現(xiàn)手段2015-12-12
詳解Angular2表單-模板驅(qū)動(dòng)的表單(Template-Driven Forms)
本篇文章主要介紹了詳解Angular2表單-模板驅(qū)動(dòng)的表單(Template-Driven Forms),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
angularJS實(shí)現(xiàn)不同視圖同步刷新詳解
今天小編就為大家分享一篇angularJS實(shí)現(xiàn)不同視圖同步刷新詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
AngularJS實(shí)現(xiàn)網(wǎng)站換膚實(shí)例
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)網(wǎng)站換膚的簡(jiǎn)單實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
一篇文章快速了解Angular和Ionic生命周期和鉤子函數(shù)
Ionic以AngularJS和ApacheCordova為基礎(chǔ),使用Node.js進(jìn)行模塊管理,使用Html5、Css(SASS)和Javascript技術(shù)進(jìn)行APP開(kāi)發(fā),這篇文章主要給大家介紹了如何通過(guò)一篇文章快速了解Angular和Ionic生命周期和鉤子函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-07-07

