AngularJS实现自定义指令及指令配置项的方法
程序员文章站
2022-06-18 08:29:50
本文实例讲述了angularjs实现自定义指令及指令配置项的方法。分享给大家供大家参考,具体如下:
angularjs自定义指令有两种写法:
//第一种
an...
本文实例讲述了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"; }]);
更多关于angularjs相关内容感兴趣的读者可查看本站专题:《angularjs指令操作技巧总结》、《angularjs入门与进阶教程》及《angularjs mvc架构总结》
希望本文所述对大家angularjs程序设计有所帮助。
推荐阅读
-
vue.js通过自定义指令实现数据拉取更新的实现方法
-
AngularJs1.x自定义指令独立作用域的函数传入参数方法
-
AngularJs中 ng-repeat指令中实现含有自定义指令的动态html的方法
-
在 Angular2 中实现自定义校验指令(确认密码)的方法
-
Angularjs使用directive自定义指令实现attribute继承的方法详解
-
Vue自定义指令实现checkbox全选功能的方法
-
AngularJS使用带属性值的ng-app指令实现自定义模块自动加载的方法
-
AngularJS框架的ng-app指令与自动加载实现方法分析
-
AngularJS使用自定义指令替代ng-repeat的方法
-
Vue2.x Todo之自定义指令实现自动聚焦的方法