angular directive的簡單使用總結(jié)
摘要
directive(指令)是angular的一個非常強大又有用的功能,它相當于實現(xiàn)了組件化的概念。我們可以通過它公共地自定義DOM元素或CLASS類或ATTR屬性,并且在這基礎(chǔ)上進行操作scope、綁定事件等等。將一些公共的模塊或操作封裝到指令中,然后就可以在html頁面中編寫簡單的一行代碼就可以加載整個公共模塊,大大避免了代碼的冗余。一般使用directive有以下場景:
- 使html更具有語義化,不需要深入研究和了解邏輯即可知道頁面的大致邏輯;
- 抽象出一個自定義的組件,以便在其他地方可以進行復用。
下面我想通過一些實例結(jié)合分析對我所了解的directive進行一些簡單的歸納總結(jié)(我所使用的是angular1.5.3):
一、Directive的使用
angular.module("app",[]).directive("directiveName",function(){ return{ //通過設(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 }; });
效果截圖:
實例解析:
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、默認情況下,directive將僅僅允許通過屬性聲明,ECA較常用。
template:指令顯示的模板內(nèi)容,一般由html標簽和文本組成。通常情況下html內(nèi)容較簡單的情況下使用,模板內(nèi)容復雜的建議將公共部分抽離到html文件中,使用templateUrl屬性。
templateUrl - 與template基本一致,但模版通過指定的url進行加載。因為模版加載是異步的,所以compilation、linking都會暫停,等待加載完畢后再執(zhí)行。
3、replace:如果設(shè)置為true,那么模版將會替換當前元素,而不是作為子元素添加到當前元素中。(注:為true時,模版必須有一個根節(jié)點)
上述實例dom節(jié)點截圖:
三、實例2:關(guān)于transclude
修改上面實例代碼:
<!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:指令的作用是把我們自定義的語義化標簽替換成瀏覽器能夠認識的HTML標簽。上述例子replace設(shè)置為true,模版將會替換當前元素。而transclude設(shè)置為true的作用可以簡化地理解成:把<hello>標簽替換成我們所編寫的HTML模板,但是<hello>標簽內(nèi)部的內(nèi)容保持不變。而<span ng-transclude></span>則是指明標簽內(nèi)部內(nèi)容插入到模板內(nèi)容的哪個位置。
四、實例3:關(guān)于compile,link和controller
實例代碼:
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>
運行結(jié)果:
Hello 1111 22222 44444 55555!
由結(jié)果可以看出來,controller先運行,compile后運行,link不運行。
將上例中的compile注釋掉的運行結(jié)果:
Hello 1111 22222 33333!
由結(jié)果可以看出來,controller先運行,link后運行,link和compile不兼容。
簡單解析:
指令的控制器和link函數(shù)(后面會講)可以進行互換。區(qū)別在于,控制器主要是用來提供可在指令間復用的行為但link鏈接函數(shù)只能在當前內(nèi)部指令中定義行為,且無法再指令間復用。
更多了解可以參考Angular官方站點:https://angularjs.org/
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Angular2表單-模板驅(qū)動的表單(Template-Driven Forms)
本篇文章主要介紹了詳解Angular2表單-模板驅(qū)動的表單(Template-Driven Forms),具有一定的參考價值,有興趣的可以了解一下2017-08-08一篇文章快速了解Angular和Ionic生命周期和鉤子函數(shù)
Ionic以AngularJS和ApacheCordova為基礎(chǔ),使用Node.js進行模塊管理,使用Html5、Css(SASS)和Javascript技術(shù)進行APP開發(fā),這篇文章主要給大家介紹了如何通過一篇文章快速了解Angular和Ionic生命周期和鉤子函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-07-07