Angular的自定義指令以及實(shí)例
前面的文章介紹了很多angular自帶的指令,下面我們看看如何使用directive自定義指令。
先看一個(gè)例子:
<body> <div my-hello></div> </body> <script type="text/javascript"> var m1 = angular.module('myApp',[]); m1.directive('myHello',function(){ return { restrict : 'A', replace : true, template : '<div>hello angular</div>' }; }); </script>
1:我們定義了一個(gè)my-hello的指令。
2:使用directive完善這個(gè)指令,返回一個(gè)對象。有三個(gè)值:
a) :restrict共四個(gè)值:E:標(biāo)簽指令,C:class指令,M:注釋指令,A:屬性指令
如何使用 ?
b):replace是否替換(M注釋必須為true才能解析)看圖:
true:
false:
c):template內(nèi)容,除此之外還有templateUrl,指定一個(gè)html模板文件。
下面再舉個(gè)例子:
<div ng-controller="Aaa"> <div my-tab my-id="div1" my-name="name" my-fn="show(num)" class="J-tab"></div> <div my-tab my-id="div2" my-name="name" my-fn="show(num)" class="J-tab"></div> </div> <script type="text/javascript"> var m1 = angular.module('myApp',[]); m1.controller('Aaa',['$scope',function($scope){ $scope.name = 'xiecg'; $scope.age = 18; $scope.show = function(num){ console.log(num); }; }]); m1.directive('myTab',function(){ return { restrict : 'ECMA', replace : true, //替換的方式插入內(nèi)容//綁定策略 scope : { myId : '@', //解析普通字符串 myName : '=', //解析數(shù)據(jù) myFn : '&' //函數(shù) }, controller : ['$scope',function($scope){ //共享數(shù)據(jù)存放在這里 $scope.name = 'this is a xiecg'; }], template : '<div id="{{myId}}">\ <input type="button" value="1" class="active" ng-click="myFn({num:456})">\ <input type="button" value="2">\ <input type="button" value="3">\ <div style="display:block;">{{myName}}</div>\ <div>2222</div>\ <div>3333</div>\ </div>' }; }); </script>
1:scope默認(rèn)是false,為true表示獨(dú)立作用域。
2:scope給予一個(gè)對象時(shí),表示執(zhí)行綁定策略,在template上調(diào)用這些數(shù)據(jù)。
a):我們在DOM元素上my-id,我們使用@符號(hào),表示解析普通字符串,說白了就是你寫什麼就是什麼。
b):使用=符號(hào),表示解析數(shù)據(jù)。
c):使用&符號(hào),表示這綁定一個(gè)函數(shù)。
3:controller,表示綁定指令內(nèi)部使用的數(shù)據(jù)。
好,下面來繼續(xù)完善這個(gè)tab切換的例子!
完整代碼:
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Tab選項(xiàng)卡實(shí)例</title> <style type="text/css"> .J-tab .active{background-color:#03A9F4;} .J-tab div{display:none;} </style> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <script type="text/javascript" src="js/angular.min.js"></script> </head> <body> <div ng-controller="Aaa"> <my-tab my-id="div1" my-data="sports" class="J-tab"></my-tab> <my-tab my-id="div2" my-data="time" class="J-tab"></my-tab> </div> <script type="text/javascript"> var m1 = angular.module('myApp',[]); m1.controller('Aaa',['$scope',function($scope){ $scope.sports = [ {title : '籃球',content : '111111111'}, {title : '足球',content : '222222222'}, {title : '排球',content : '333333333'} ]; $scope.time = [ {title : '上午',content : '444444444'}, {title : '中午',content : '555555555'} ]; }]); m1.directive('myTab',function(){ return { restrict : 'E', replace : true, scope : { myId : '@', myData : '=' }, controller : ['$scope',function($scope){ $scope.name = 'this is a xiecg'; }], template : '<div id="{{myId}}">\ <input ng-repeat="data in myData" type="button" ng-value="data.title" ng-class="{active:$first}">\ <div ng-repeat="data in myData" ng-style="{display:$first?\'block\':\'none\'}">{{data.content}}</div>\ </div>', link : function(scope,element,attr){ element.on('click','input',function(){ var self = $(this) , i = self.index(); self.addClass('active').siblings('input').removeClass('active'); self.siblings('div').eq(i).show().siblings('div').hide(); }); } }; }); </script> </body> </html>
link屬性,表示當(dāng)directive被angular編譯后,執(zhí)行該方法。這個(gè)方法接受三個(gè)參數(shù),
a):scope表示controller下面的數(shù)據(jù)。
b):element表示當(dāng)前的DOM元素。
c):attr表示這個(gè)DOM元素上的自定義屬性。
補(bǔ)充:
在實(shí)際的開發(fā)過程中我們往往需要嵌套各種組件和指令。下面來介紹directive中的transclude和require。
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>自定義指令間的互相交互</title> <script type="text/javascript" src="js/angular.min.js"></script> </head> <body> <div> <hello> <hi></hi> </hello> </div> <script type="text/javascript"> var m1 = angular.module('myApp',[]); m1.directive('hello',function(){ return { restrict : 'E', replace : true, transclude : true, //允許自定義指令的嵌套,通過ng-transclude指定嵌套的范圍 controller : function($scope){ $scope.name = 'xiecg'; this.name = 'xiecg'; //使用this共享給其他指令 }, template : '<div>hello angular <h1 ng-transclude></h1></div>' }; }); m1.directive('hi',function(){ return { restrict : 'E', replace : true, require : '^hello',//hello指令屬性hi指令的父級(jí),需要用^符號(hào)指定。如果無法指定,使用?容錯(cuò)處理。 template : '<span>hi angular {{name}}</span>', link : function(scope,element,attr,reController){ console.log(reController); //得到父級(jí)hello指令中共享出來的數(shù)據(jù) } }; }); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- Angular.js自定義指令學(xué)習(xí)筆記實(shí)例
- Angularjs自定義指令實(shí)現(xiàn)三級(jí)聯(lián)動(dòng) 選擇地理位置
- 詳解angular2采用自定義指令(Directive)方式加載jquery插件
- AngularJs中 ng-repeat指令中實(shí)現(xiàn)含有自定義指令的動(dòng)態(tài)html的方法
- AngularJS創(chuàng)建自定義指令的方法詳解
- angularjs 表單密碼驗(yàn)證自定義指令實(shí)現(xiàn)代碼
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS 自定義指令詳解及實(shí)例代碼
- 深入講解AngularJS中的自定義指令的使用
- 詳解AngularJS中自定義指令的使用
- Angular1.x自定義指令實(shí)例詳解
相關(guān)文章
使用yeoman構(gòu)建angular應(yīng)用的方法
下面小編就為大家?guī)硪黄褂脃eoman構(gòu)建angular應(yīng)用的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08探討AngularJs中ui.route的簡單應(yīng)用
這篇文章主要介紹了AngularJs中ui.route的簡單應(yīng)用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Angular Universal服務(wù)器端渲染避免 window is not&
這篇文章主要介紹了Angular Universal服務(wù)器端渲染避免 window is not defined錯(cuò)誤消息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07深究AngularJS如何獲取input的焦點(diǎn)(自定義指令)
本篇文章主要介紹了AngularJS如何獲取input的焦點(diǎn)(自定義指令),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Angular中$cacheFactory的作用和用法實(shí)例詳解
$cacheFactory是一個(gè)為Angular服務(wù)生產(chǎn)緩存對象的服務(wù)。接下來通過本文給大家介紹Angular中$cacheFactory的作用和用法實(shí)例詳解,非常不錯(cuò),感興趣的朋友一起看下吧2016-08-08