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í)驗證明,如果改變這個參數(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)文章
Angular2中select用法之設(shè)置默認(rèn)值與事件詳解
在Angular.JS中可以使用數(shù)組或?qū)ο髣?chuàng)建一個下拉列表選項。關(guān)于Angular.js中select的基礎(chǔ)相信大家應(yīng)該都已經(jīng)了解了,那么下面這篇文章主要給大家介紹了Angular2中select用法之設(shè)置默認(rèn)值與事件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
angular2 組件之間通過service互相傳遞的實(shí)例
今天小編就為大家分享一篇angular2 組件之間通過service互相傳遞的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Angular7實(shí)現(xiàn)拖放Drag?Drop示例詳解
這篇文章主要介紹了Angular7實(shí)現(xiàn)拖放Drag?Drop示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
AngularJS獲取json數(shù)據(jù)的方法詳解
這篇文章主要介紹了AngularJS獲取json數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了AngularJS獲取json數(shù)據(jù)的詳細(xì)步驟、操作技巧與相關(guān)注意事項,需要的朋友可以參考下2017-05-05
AngularJS中$watch和$timeout的使用示例
這篇文章給大家介紹了AngularJS中$watch和$timeout的使用例子,通過示例代碼相信更能讓大家理解,有需要的朋友們下面來一起看看吧。2016-09-09

