Angular.js自定義指令學習筆記實例
更新時間:2017年02月24日 09:47:40 作者:焦大叔
這篇文章主要介紹了Angular.js自定義指令的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
本文給大家分享angular.js學習筆記之自定義指令實例代碼講解,具體代碼如下所示:
<!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>如果采用雙向綁定,指令中的屬性值默認是變量,所以不用添加{{}}
<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自定義指令的格式和相關參數(shù)與其值:
let m=angular.module('angularJS',[]);
m.directive('selfDirect',[function(){//selfDirect表示自定義指令的名字,采用駝峰命名法,當restrict的值為E的時候:<self-direct></self-direct>
return {
restrict:'A/E/C',//A:Attrabute,E:Elements,C:class;restrict屬性表示生成指令在頁面中的表現(xiàn)形式,字母必須大寫,不建議使用C,因為C的寫法與CSS耦合性太強.
template:'<p>template選項表示指令在頁面中顯示的內(nèi)容,template的值可以是字符串也可以是HTML的標簽形式,也可以為函數(shù),如:template:function(elle,attr){return '<span style="'color:'+attr['color']+'">'+ele.html()+'</span>'},view內(nèi)容太多的時候不建議使用函數(shù)的形式</p>',
replace:true,//使用模板內(nèi)容替換包含模板內(nèi)容的父級標簽
transclude:true,//其內(nèi)容填充到ng-transclude指定的位置
templateUrl:'',//不可與template同時使用
scope:true,//默認為false,設置指令的作用域,當值為{}時,模板中的變量不會繼承來自控制器中的屬性值,
controller:['$scope',function($scope){$scope.data={...}}],//指令中的控制器
link:function(scope,elem,attr){},//用link完成對DOM的操作,scope:指令的作用域,elem:指令標簽元素,attr:指令標簽元素的屬性數(shù)組,
};
}])
*/
var m=angular.module('angularJS',[]);
m.directive('selfDirect', [function () {
return {
restrict: 'E',
//template:'<h1><span ng-transclude=""></span>This is a Angular.js direction of self definition</h1><div ng-transclude=""></div>',
//replace:true,
//transclude:true,
//templateUrl:'viewModel.html',
//scope:{},
//template:'{{title}}<input type="text" ng-model="title">',
//template:'<p style="color:{{color}}">suNing store</p><input ng-model="color">',
//scope:{color:'@mColor'},//控制器和指令隔離作用域@單項文本綁定,控制器可以影響指令中的數(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>
以上所述是小編給大家介紹的Angular.js自定義指令的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
- Angular1.x自定義指令實例詳解
- Angular之指令Directive用法詳解
- 詳解Angular.js指令中scope類型的幾種特殊情況
- angular分頁指令操作
- AngularJS內(nèi)置指令
- AngularJS學習筆記之基本指令(init、repeat)
- AngularJS中的指令全面解析(必看)
- 用AngularJS的指令實現(xiàn)tabs切換效果
- 深入講解AngularJS中的自定義指令的使用
- Angularjs編寫KindEditor,UEidtor,jQuery指令
- 自定義Angular指令與jQuery實現(xiàn)的Bootstrap風格數(shù)據(jù)雙向綁定的單選與多選下拉框
- Angular1.x復雜指令實例詳解
相關文章
angular2+node.js express打包部署的實戰(zhàn)
本篇文章主要介紹了angular2+node.js express打包部署的實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
詳解angularjs的數(shù)組傳參方式的簡單實現(xiàn)
本篇文章主要介紹了angularjs的數(shù)組傳參方式的簡單實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
AngularJS對動態(tài)增加的DOM實現(xiàn)ng-keyup事件示例
這篇文章主要介紹了AngularJS對動態(tài)增加的DOM實現(xiàn)ng-keyup事件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Angular中實現(xiàn)樹形結(jié)構(gòu)視圖實例代碼
近兩年當中使用Angular開發(fā)過很多項目,其中也涉及到一些樹形結(jié)構(gòu)視圖的顯示,最近的在項目中封裝了大量的組件,一些組件也有樹形結(jié)構(gòu)的展示,所以寫出來總結(jié)一下。2017-05-05
AngularJS實現(xiàn)動態(tài)切換樣式的方法分析
這篇文章主要介紹了AngularJS實現(xiàn)動態(tài)切換樣式的方法,結(jié)合實例形式分析了AngularJS事件響應與樣式動態(tài)控制相關操作技巧,需要的朋友可以參考下2018-06-06

