AngularJS 指令的交互詳解及實(shí)例代碼
背景介紹
這例子是視頻中的例子,有一個動感超人,有三種能力,力量strength,速度speed,發(fā)光light。
這三種能力作為三種屬性,定義動感超人作為一個標(biāo)簽,只要添加對應(yīng)的屬性就能擁有該能力。
為了便于結(jié)果的展示,為標(biāo)簽添加鼠標(biāo)的響應(yīng)事件,當(dāng)鼠標(biāo)移動到對應(yīng)的標(biāo)簽上就會觸發(fā)一個方法,打印出具備的能力。
程序分析
html部分的代碼如下:
<div> <superman>nothing!</superman> <superman strength >strength!</superman> <superman strength speed >strength speed!</superman> <superman strength speed light >strength speed light!</superman> </div>
下面看看如何實(shí)現(xiàn),首先依然是創(chuàng)建一個模塊:
var myAppModule = angular.module("myApp",[]);
在該模塊的基礎(chǔ)上,創(chuàng)建標(biāo)簽superman,與前面類似。
myAppModule.directive("superman",function(){ return{ scope:{}, restrict:'AE', transclude:true, template:"<div><div ng-transclude></div></div>", controller:function($scope){ $scope.abilities = []; this.addStrength = function(){ $scope.abilities.push("strength"); }; this.addSpeed = function(){ $scope.abilities.push("speed"); }; this.addLight = function(){ $scope.abilities.push("light"); }; }, link:function(scope,element,attr){ element.bind("mouseenter",function(){ console.log(scope.abilities); }); } } });
這里不同的是,在方法內(nèi)部有一個controller屬性,這個并不是ng-controller這種控制器,而是指令對外開放的一個接口,里面聲明的方法,在外部可以作為公開的方法使用,其他的指令可以通過依賴,使用這些方法。
接下來再創(chuàng)建三個能力對應(yīng)的指令
myAppModule.directive("strength",function(){ return{ require:'^superman', link:function(scope,element,attr,supermanCtrl){ supermanCtrl.addStrength(); } } }); myAppModule.directive("speed",function(){ return{ require:'^superman', link:function(scope,element,attr,supermanCtrl){ supermanCtrl.addSpeed(); } } }); myAppModule.directive("light",function(){ return{ require:'^superman', link:function(scope,element,attr,supermanCtrl){ supermanCtrl.addLight(); } } });
三個指令的代碼都差不多,其中require指定了依賴的指令。
link中多了一個參數(shù)supermanCtrl,這個參數(shù)猜想是superman中的controller,所以命名采用superman+Ctrl的方式。
【由于不懂內(nèi)部原理,這里僅僅是猜想,但是實(shí)驗(yàn)證明,如果改變這個參數(shù)的名字,會報錯?!?/p>
聲明了這三個指令,就可以把這三個指令當(dāng)做super的屬性來使用,當(dāng)注明該屬性時,就會觸發(fā)內(nèi)部的link內(nèi)的方法,調(diào)用superman中公開的方法?! ?/p>
總結(jié)起來,指令的交互過程:
1 首先創(chuàng)建一個基本的指令,在controller屬性后,添加對外公開的方法。
2 創(chuàng)建其他交互的指令,在require屬性后,添加對應(yīng)的指令依賴關(guān)系;在link中調(diào)用公開的方法
全部程序代碼:
<!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> <superman>nothing!</superman> <superman strength >strength!</superman> <superman strength speed >strength speed!</superman> <superman strength speed light >strength speed light!</superman> </div> <script type="text/javascript"> var myAppModule = angular.module("myApp",[]); myAppModule.directive("superman",function(){ return{ scope:{}, restrict:'AE', transclude:true, template:"<div><div ng-transclude></div></div>", controller:function($scope){ $scope.abilities = []; this.addStrength = function(){ $scope.abilities.push("strength"); }; this.addSpeed = function(){ $scope.abilities.push("speed"); }; this.addLight = function(){ $scope.abilities.push("light"); }; }, link:function(scope,element,attr){ element.bind("mouseenter",function(){ console.log(scope.abilities); }); } } }); myAppModule.directive("strength",function(){ return{ require:'^superman', link:function(scope,element,attr,supermanCtrl){ supermanCtrl.addStrength(); } } }); myAppModule.directive("speed",function(){ return{ require:'^superman', link:function(scope,element,attr,supermanCtrl){ supermanCtrl.addSpeed(); } } }); myAppModule.directive("light",function(){ return{ require:'^superman', link:function(scope,element,attr,supermanCtrl){ supermanCtrl.addLight(); } } }); </script> </body> </html>
運(yùn)行結(jié)果:
以上就是對AngularJS 指令的交互的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!
- angularJS之$http:與服務(wù)器交互示例
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- AngularJS指令與指令之間的交互功能示例
- AngularJS指令與控制器之間的交互功能示例
- AngularJS中directive指令使用之事件綁定與指令交互用法示例
- AngularJS實(shí)現(xiàn)與Java Web服務(wù)器交互操作示例【附demo源碼下載】
- AngularJS入門教程之與服務(wù)器(Ajax)交互操作示例【附完整demo源碼下載】
- 詳解AngularJs中$resource和restfu服務(wù)端數(shù)據(jù)交互
- angularjs實(shí)現(xiàn)與服務(wù)器交互分享
- AngularJS中$http的交互問題
相關(guān)文章
AngularJS基礎(chǔ) ng-cloak 指令簡單示例
本文主要介紹AngularJS ng-cloak 指令,這里幫大家整理了ng-clock指令的基礎(chǔ)資料,和簡單的代碼實(shí)例及效果圖,學(xué)習(xí)AngularJS指令的朋友可以參考下2016-08-08Angular 4.x+Ionic3踩坑之Ionic3.x pop反向傳值詳解
這篇文章主要給大家介紹了關(guān)于Angular 4.x+Ionic3踩坑之Ionic3.x pop反向傳值的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03MODULE_INITIALIZER初始化Angular?懶加載模塊高級技巧
這篇文章主要為大家介紹了MODULE_INITIALIZER初始化Angular懶加載模塊高級技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Angular實(shí)現(xiàn)下拉框模糊查詢功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)下拉框模糊查詢功能,涉及AngularJS事件響應(yīng)及字符串查詢等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01用AngularJS來實(shí)現(xiàn)監(jiān)察表單按鈕的禁用效果
本篇文章主要介紹了用AngularJS來實(shí)現(xiàn)監(jiān)察表單按鈕的禁用效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11angular 實(shí)時監(jiān)聽input框value值的變化觸發(fā)函數(shù)方法
今天小編就為大家分享一篇angular 實(shí)時監(jiān)聽input框value值的變化觸發(fā)函數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08在AngularJS應(yīng)用中實(shí)現(xiàn)一些動畫效果的代碼
這篇文章主要介紹了在AngularJS應(yīng)用中實(shí)現(xiàn)一些動畫效果的代碼,AngularJS是一款熱門的JavaScript庫,需要的朋友可以參考下2015-06-06AngualrJS中每次$http請求時的一個遮罩層Directive
AngularJS是一款非常強(qiáng)大的前端MVC框架。接下來通過本文給大家介紹AngualrJS中每次$http請求時的一個遮罩層Directive,本文非常具有參考借鑒價值,特此分享供大家學(xué)習(xí)2016-01-01