Angular的自定義指令以及實例
前面的文章介紹了很多angular自帶的指令,下面我們看看如何使用directive自定義指令。
先看一個例子:
<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:我們定義了一個my-hello的指令。
2:使用directive完善這個指令,返回一個對象。有三個值:
a) :restrict共四個值:E:標(biāo)簽指令,C:class指令,M:注釋指令,A:屬性指令
如何使用 ?

b):replace是否替換(M注釋必須為true才能解析)看圖:
true:
false:
c):template內(nèi)容,除此之外還有templateUrl,指定一個html模板文件。
下面再舉個例子:
<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默認是false,為true表示獨立作用域。
2:scope給予一個對象時,表示執(zhí)行綁定策略,在template上調(diào)用這些數(shù)據(jù)。
a):我們在DOM元素上my-id,我們使用@符號,表示解析普通字符串,說白了就是你寫什麼就是什麼。
b):使用=符號,表示解析數(shù)據(jù)。
c):使用&符號,表示這綁定一個函數(shù)。
3:controller,表示綁定指令內(nèi)部使用的數(shù)據(jù)。

好,下面來繼續(xù)完善這個tab切換的例子!
完整代碼:
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tab選項卡實例</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í)行該方法。這個方法接受三個參數(shù),
a):scope表示controller下面的數(shù)據(jù)。
b):element表示當(dāng)前的DOM元素。
c):attr表示這個DOM元素上的自定義屬性。
補充:
在實際的開發(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指令的父級,需要用^符號指定。如果無法指定,使用?容錯處理。
template : '<span>hi angular {{name}}</span>',
link : function(scope,element,attr,reController){
console.log(reController); //得到父級hello指令中共享出來的數(shù)據(jù)
}
};
});
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- Angular.js自定義指令學(xué)習(xí)筆記實例
- Angularjs自定義指令實現(xiàn)三級聯(lián)動 選擇地理位置
- 詳解angular2采用自定義指令(Directive)方式加載jquery插件
- AngularJs中 ng-repeat指令中實現(xiàn)含有自定義指令的動態(tài)html的方法
- AngularJS創(chuàng)建自定義指令的方法詳解
- angularjs 表單密碼驗證自定義指令實現(xiàn)代碼
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS 自定義指令詳解及實例代碼
- 深入講解AngularJS中的自定義指令的使用
- 詳解AngularJS中自定義指令的使用
- Angular1.x自定義指令實例詳解
相關(guān)文章
使用yeoman構(gòu)建angular應(yīng)用的方法
下面小編就為大家?guī)硪黄褂脃eoman構(gòu)建angular應(yīng)用的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
探討AngularJs中ui.route的簡單應(yīng)用
這篇文章主要介紹了AngularJs中ui.route的簡單應(yīng)用,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Angular Universal服務(wù)器端渲染避免 window is not&
這篇文章主要介紹了Angular Universal服務(wù)器端渲染避免 window is not defined錯誤消息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
深究AngularJS如何獲取input的焦點(自定義指令)
本篇文章主要介紹了AngularJS如何獲取input的焦點(自定義指令),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Angular中$cacheFactory的作用和用法實例詳解
$cacheFactory是一個為Angular服務(wù)生產(chǎn)緩存對象的服務(wù)。接下來通過本文給大家介紹Angular中$cacheFactory的作用和用法實例詳解,非常不錯,感興趣的朋友一起看下吧2016-08-08

