AngularJS实现自定义指令及指令配置项的方法
更新时间:2017年11月20日 11:27:00 作者:fallstar
这篇文章主要介绍了AngularJS实现自定义指令及指令配置项的方法,结合实例形式简单总结分析了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通过$location获取及改变当前页面的URL
本篇将介绍AngularJS中的$location服务的基本用法,$location服务的主要作用是用于获取当前url以及改变当前的url,并且存入历史记录。本文通过示例代码介绍的很详细,有需要的朋友们可以参考借鉴,下面来一起看看吧。2016-09-09AngularJS基于ngInfiniteScroll实现下拉滚动加载的方法
这篇文章主要介绍了AngularJS基于ngInfiniteScroll实现下拉滚动加载的方法,结合实例形式分析AngularJS下拉滚动插件ngInfiniteScroll的下载、功能、属性及相关使用方法,需要的朋友可以参考下2016-12-12angularJs使用$watch和$filter过滤器制作搜索筛选实例
本篇文章主要介绍了angularJs使用$watch和$filter过滤器制作搜索筛选实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06Angular2管道Pipe及自定义管道格式数据用法实例分析
这篇文章主要介绍了Angular2管道Pipe及自定义管道格式数据用法,结合实例形式详细分析了Angular2管道与纯管道相关概念、语法及使用技巧,需要的朋友可以参考下2017-11-11
最新评论