最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Angular.js自定义指令学习笔记实例
时间:2017-05-01 编辑:简简单单 来源:一聚教程网
代码如下 | 复制代码 |
/*Angular.js自定义指令的格式和相关参数与其值: let m=angular.module('angularJS',[]); m.directive('selfDirect',[function(){//selfDirect表示自定义指令的名字,采用驼峰命名法,当restrict的值为E的时候: return { restrict:'A/E/C',//A:Attrabute,E:Elements,C:class;restrict属性表示生成指令在页面中的表现形式,字母必须大写,不建议使用C,因为C的写法与CSS耦合性太强. template:' template选项表示指令在页面中显示的内容,template的值可以是字符串也可以是HTML的标签形式,也可以为函数,如:template:function(elle,attr){return ''+ele.html()+''},view内容太多的时候不建议使用函数的形式 ',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:指令标签元素的属性数组, }; }]) */ varm=angular.module('angularJS',[]); m.directive('selfDirect', [function () { return { restrict: 'E', //template:' This is a Angular.js direction of self definition',//replace:true, //transclude:true, //templateUrl:'viewModel.html', //scope:{}, //template:'{{title}}', //template:' suNing store ',//scope:{color:'@mColor'},//控制器和指令隔离作用域@单项文本绑定,控制器可以影响指令中的数据,而指令不能影响控制器中的数据 //scope:{color:'=mColor'},//控制器和指令隔离作用域=双向文本绑定,控制器可以影响指令中的数据,指令也可以影响控制器中的data //template:' {{logo()}} ',//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'; }; }]);*/ |
-
下一个: js获取隐藏元素的宽高
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31