当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > Angular.js自定义指令学习笔记实例

Angular.js自定义指令学习笔记实例

2018年05月01日  | 移动技术网IT编程  | 我要评论

本文给大家分享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自定义指令的格式和相关参数与其值:
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属性表示生成指令在页面中的表现形式,字母必须大写,不建议使用c,因为c的写法与css耦合性太强.
template:'<p>template选项表示指令在页面中显示的内容,template的值可以是字符串也可以是html的标签形式,也可以为函数,如:template:function(elle,attr){return '<span style="'color:'+attr['color']+'">'+ele.html()+'</span>'},view内容太多的时候不建议使用函数的形式</p>',
replace:true,//使用模板内容替换包含模板内容的父级标签
transclude:true,//其内容填充到ng-transclude指定的位置
templateurl:'',//不可与template同时使用
scope:true,//默认为false,设置指令的作用域,当值为{}时,模板中的变量不会继承来自控制器中的属性值,
controller:['$scope',function($scope){$scope.data={...}}],//指令中的控制器
link:function(scope,elem,attr){},//用link完成对dom的操作,scope:指令的作用域,elem:指令标签元素,attr:指令标签元素的属性数组,
};
}])
*/
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'},//控制器和指令隔离作用域@单项文本绑定,控制器可以影响指令中的数据,而指令不能影响控制器中的数据
//scope:{color:'=mcolor'},//控制器和指令隔离作用域=双向文本绑定,控制器可以影响指令中的数据,指令也可以影响控制器中的data
//template:'<p>{{logo()}}</p>',
//scope:{logo:'&'},//用&符号调用父控制器中的方法
/*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自定义指令的实例代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网