【发布时间】:2015-09-15 02:13:36
【问题描述】:
我有一个指令,我需要在指令控制器中访问其“config”属性值。 由于首先执行控制器构造函数,因此可以从控制器到链接进行通信,但反之则不行。 实现这一目标的最佳方法应该是什么? 我考虑了以下方法
1) 将变量添加到作用域- 在我看来,这会污染范围,使变量在共享范围的任何其他地方都可以访问。
2)使用$broadcast
又是和上面一样的问题
3) 在控制器的this 上传递一个回调函数,并以config 作为参数从链接函数调用它
4)通过服务传递值-在我的情况下,我有多个此类指令需要通过该服务传递日期
或者有没有更好的方法让我错过了这样做?
module.directive('myDirective',function(){
return{
restrict:'E',
templateUrl:'path/to/html',
link:function(scope,iElement,iAttrs,controller){
var config=iAttrs.config;
//How to access this value inside the directive controller?
},
controller:function($scope){
//the directive attribute 'config' is required here for some larger computations which are not
//manipulating the DOM and hence should be seperated from the link function
})
【问题讨论】:
-
您是否尝试在控制器中注入 $attrs?
-
任何描述其用法的链接将不胜感激。文档对 $attrs 不是很清楚
标签: javascript angularjs angularjs-directive dom-manipulation