本文主要介绍了angularjs实现自定义指令及指令配置项的方法,结合实例形式简单总结分析了angularjs自定义指令及指令配置项的实现技巧,需要的朋友可以参考下,希望能帮助到大家。
AngularJS自定义指令有两种写法:
//第一种angular.module('MyApp',[]).directive('zl1',zl1).controller('con1',['$scope',func1]);function zl1(){ var directive={ restrict:'AEC', template:'this is the it-first directive', }; return directive;};function func1($scope){ $scope.name="alice";}//第二种angular.module('myApp',[]).directive('zl1',[ function(){ return { restrict:'AE', template:'thirective', link:function($scope,elm,attr,controller){ console.log("这是link"); }, controller:function($scope,$element,$attrs){ console.log("这是con"); } };}]).controller('Con1',['$scope',function($scope){ $scope.name="aliceqqq";}]);
登录后复制
指令配置项
angular.module('myApp', []).directive('first', [ function(){ return { // scope: false, // 默认值,共享父级作用域 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment template: 'first name:{{name}}', };}]).directive('second', [ function(){ return { scope: true, // 继承父级作用域并创建指令自己的作用域 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment //当修改这里的name时,second会在自己的作用域中新建一个name变量,与父级作用域中的 // name相对独立,所以再修改父级中的name对second中的name就不会有影响了 template: 'second name:{{name}}', };}]).directive('third', [ function(){ return { scope: {}, // 创建指令自己的独立作用域,与父级毫无关系 // controller: function($scope, $element, $attrs, $transclude) {}, restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment template: 'third name:{{name}}', };}]).controller('DirectiveController', ['$scope', function($scope){ $scope.name="mike";}]);
登录后复制
相关推荐:
vue-cli 自定义指令directive 添加验证滑块详解
怎样使用Vue的自定义指令完成一个下拉菜单
关于Angularjs的自定义指令Directive的具体介绍
以上就是AngularJS实现自定义指令方法详解的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2790943.html