AngularJS实现自定义指令及指令配置项的方法
2019/6/27 20:33:20
本文主要是介绍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"; }]);
更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》
希望本文所述对大家AngularJS程序设计有所帮助。
这篇关于AngularJS实现自定义指令及指令配置项的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04package.json 文件位置在哪?-icode9专业技术文章分享
- 2024-10-01Craco.js学习:从入门到实践指南
- 2024-10-01Create-React-App学习:入门与实践指南
- 2024-10-01CSS-in-JS学习:从入门到实践指南
- 2024-09-30JSX语法学习:从入门到初步掌握
- 2024-09-30Mock.js学习:入门教程与实战演练
- 2024-09-30React Hooks学习:从入门到实践
- 2024-09-30受控组件学习:React中的基础入门教程
- 2024-09-29JS定时器教程:初学者必看指南
- 2024-09-29JS对象教程:初学者的全面指南