溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Angular.js如何自定義指令

發(fā)布時(shí)間:2021-08-11 10:55:31 來源:億速云 閱讀:102 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了Angular.js如何自定義指令,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體代碼如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularDirective</title>
<script src="http://cdn.bootcss.com/angular.js/1.4.6/angular.js"></script>
</head>
<body ng-app="angularJS" >
<!-- <div class="self-direct">{{title}}<input type="text" ng-model='title'></div> -->
<!-- <input type="text" ng-model="color">
<self-direct color='{{color}}'></self-direct>
<self-direct m-color='{{color}}'></self-direct> -->
<!-- <input type="text" ng-model="color">
<self-direct color='color'></self-direct>如果采用雙向綁定,指令中的屬性值默認(rèn)是變量,所以不用添加{{}}
<self-direct m-color='color'></self-direct> -->
<!-- <self-direct logo='logo()'></self-direct> -->
<!-- <self-direct ></self-direct> -->
<!-- <self-direct ></self-direct> -->
<self-direct title="JinDong" bgcolor="red" fontcolor="#fff"></self-direct>
<script type="text/javascript">
/*Angular.js自定義指令的格式和相關(guān)參數(shù)與其值:
let m=angular.module('angularJS',[]);
m.directive('selfDirect',[function(){//selfDirect表示自定義指令的名字,采用駝峰命名法,當(dāng)restrict的值為E的時(shí)候:<self-direct></self-direct>
return {
restrict:'A/E/C',//A:Attrabute,E:Elements,C:class;restrict屬性表示生成指令在頁面中的表現(xiàn)形式,字母必須大寫,不建議使用C,因?yàn)镃的寫法與CSS耦合性太強(qiáng).
template:'<p>template選項(xiàng)表示指令在頁面中顯示的內(nèi)容,template的值可以是字符串也可以是HTML的標(biāo)簽形式,也可以為函數(shù),如:template:function(elle,attr){return '<span >'+ele.html()+'</span>'},view內(nèi)容太多的時(shí)候不建議使用函數(shù)的形式</p>',
replace:true,//使用模板內(nèi)容替換包含模板內(nèi)容的父級標(biāo)簽
transclude:true,//其內(nèi)容填充到ng-transclude指定的位置
templateUrl:'',//不可與template同時(shí)使用
scope:true,//默認(rèn)為false,設(shè)置指令的作用域,當(dāng)值為{}時(shí),模板中的變量不會繼承來自控制器中的屬性值,
controller:['$scope',function($scope){$scope.data={...}}],//指令中的控制器
link:function(scope,elem,attr){},//用link完成對DOM的操作,scope:指令的作用域,elem:指令標(biāo)簽元素,attr:指令標(biāo)簽元素的屬性數(shù)組,
};
}])
*/
var m=angular.module('angularJS',[]);
m.directive('selfDirect', [function () {
return {
restrict: 'E',
//template:'<h2><span ng-transclude=""></span>This is a Angular.js direction of self definition</h2><div ng-transclude=""></div>',
//replace:true,
//transclude:true,
//templateUrl:'viewModel.html',
//scope:{},
//template:'{{title}}<input type="text" ng-model="title">', 
//template:'<p >suNing store</p><input ng-model="color">',
//scope:{color:'@mColor'},//控制器和指令隔離作用域@單項(xiàng)文本綁定,控制器可以影響指令中的數(shù)據(jù),而指令不能影響控制器中的數(shù)據(jù)
//scope:{color:'=mColor'},//控制器和指令隔離作用域=雙向文本綁定,控制器可以影響指令中的數(shù)據(jù),指令也可以影響控制器中的data
//template:'<p>{{logo()}}</p>',
//scope:{logo:'&'},//用&符號調(diào)用父控制器中的方法
/*replace:true,
templateUrl:'viewModel.html',
controller:['$scope',function($scope){
$scope.data=[{
id:1,title:'puDong'
},{
id:2,title:'JinDong'
},{
id:3,title:'TianMao'
}];
}],*/
scope:{title:'@'},
link:function(scope,elem,attr){
$(elem).css({
backgroundColor:attr['bgcolor'],
color:attr['fontcolor']
}).html(scope.title);
},
};
}]);
/*m.controller('ctrl',['$scope',function($scope){
$scope.title='SuNing store';
$scope.color='red';
$scope.logo=function(){
return 'TianMao store';
};
}]);*/
</script>
</body>
</html>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Angular.js如何自定義指令”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI