AngularJS自定义指令controller和link传参
来源:互联网 发布:中企动力源码 编辑:程序博客网 时间:2024/05/23 00:02
指令内部controller与link传参
指令代码:
app.directive('myDirective', function() { return { restrict: 'AE', controller: function ($scope, $element, $attrs) { this.test = 1; console.log('controller设置test值: ' + this.test); }, link: function (scope, element, attrs, controller) { console.log('link收到controller的值: ' + controller.test); } }
运行结果:
自定指令中link函数的第四个参数默认为指令内部的controller,如果在该controller内定义一个自身属性,如this.test = 1; link函数就可以直接访问controller的该属性。link函数的controller参数名可以自由定义,只要link函数中该参数名和参数列表中的参数名一致即可。
外部指令controller与link函数传参
现在再定义一个指令:
html代码:
...<my-directive> <my-directive2></my-directive2></my-directive>...
指令代码:
app.directive('myDirective2', function() { return { restrict: 'AE', require: '^myDirective', link: function (scope, element, attrs, controller) { console.log('myDirective2指令中link函数收到myDirective指令的controller的值: ' + controller.test); } }});
运行结果:
这里切记两点:
1. html中第二个指令必须在第一个指令里面,也是作为它的子节点;
2. 第二个指令要访问外部指令中的 controller时,必须在require中指定要访问的指令名。
require的具体用法请参考:http://blog.csdn.net/victoryzn/article/details/76772068
阅读全文
2 0
- AngularJS自定义指令controller和link传参
- AngularJs 指令directive之controller,link,compile
- AngularJS之指令中controller与link
- AngularJs 指令directive之controller,link,compile
- AngularJs 指令directive之controller,link,compile
- angularJS 自定义指令 方法属性:controller 和属性:controllerAs
- 创建自定义 AngularJS 指令:Part 5 link
- angularJS 自定义指令 方法属性:compile、link
- angularjs指令link函数参数scope,指令controller依赖注入的$scope,与指令外部controller的$scope的关系
- angularjs指令compile、link
- angularJS directive中的controller和link function辨析
- AngularJS 世界------Angularjs指令的使用方式和自定义指令
- AngularJS之ng-controller指令
- AngularJs指令中使用controller
- angularJS自定义过滤器、服务和指令
- angularjs directive内部controller link函数理解
- Angular自定义指令之compile, link, controller属性详解及实例演示
- angularJS自定义指令
- 【前端】向blog或网站中添加语法高亮显示代码方法总结
- ios 消除 字符串 首尾空格
- 多因子模型之因子(信号)测试平台----因子值的处理(二)
- MySQL-5.6.13免安装版配置方法
- java网络爬虫
- AngularJS自定义指令controller和link传参
- 容斥原理
- C#与MySQL直接连接
- 素数筛法
- 【剑指offer】面试题 53:数字在排序数组中出现的次数
- 【学习笔记】深入理解超时调用(setTimeout)和间歇调用(setInterval)
- Restful在Eclipse调试环境中的坑SpringMVC 和 Jersey
- 多线程
- 【安卓学习笔记】JAVA基础Lesson5-关键字this