2016-03-22 53 views
0

我试过一百万种方法来从这个基于弹性的指令中访问一个rootScope变量,但没有运气。这里是指令,铭记$ rootScope确实被传递到它的父控制器。访问指令中的rootScope

var elasticui; 
(function (elasticui) { 
var directives; 
(function (directives, rootScope) { 
    var IndexDirective = (function() { 
     function IndexDirective() { 
      var directive = {}; 
      directive.restrict = 'EAC'; 
      directive.controller = elasticui.controllers.IndexController; 
      directive.link = function (scope, element, attrs, indexCtrl, rootScope) { 
      console.log("Updated Index: " + $rootScope.esIndex); 
      indexCtrl.indexVM.index = scope.$eval(attrs.euiIndex); 
      }; 
      return directive; 
     } 
     return IndexDirective; 
    })(); 
    directives.IndexDirective = IndexDirective; 
    directives.directives.directive('euiIndex', IndexDirective); 
})(directives = elasticui.directives || (elasticui.directives = {})); 
})(elasticui || (elasticui = {})); 

在其他地方我$ rootScope.esIndex设置为索引,我想指出,但我得到“$ rootScope没有定义”。我试着将指令的范围属性设置为false,尝试在$ rootScope.esIndex上设置一个观察器作为链接函数返回值的一部分,但无论我做什么,我似乎都无法弄清楚。

我认为问题的一部分是这个结构有点隐晦从一个新的角度消化的指令立场。这是一组更大的指令定义中的一个块,所以我很难掌握。

任何想法,我可以轻松抢到$ rootScope在这里?

非常感谢TON!

+0

你应该在这里肯定会提供更多的代码,但我会在初看起来你会传递'rootScope'而不是'$ rootScope'到函数中。这可能是问题的一部分。 –

回答

0

看到更多的代码会有所帮助。特别是当你说“$ rootScope确实被传递”时。然而,这种有限的信息你为什么不尝试改变这一行:

(directives = elasticui.directives || (elasticui.directives = {})); 

喜欢的东西

(directives = elasticui.directives || (elasticui.directives = {}, rootScope)); 

在它出现在你的自我执行功能一目了然没有被传递的rootScope参数它期望。祝你好运!

编辑:也

console.log("Updated Index: " + $rootScope.esIndex); 

可能不应该有美元符号。

编辑:评论有更多信息!

+0

这是下面的代码的修改,见行179 https://github.com/YousefED/ElasticUI/blob/master/dist/elasticui.js 我想要的东西,如落得: indexCtrl.indexVM.index = $ rootScope.esIndex; 如果我在页面的控制器中注销$ rootScope.esIndex,我会得到正确的值,看起来rootScope确实被传递到上面的父控制器中,但是我似乎无法从指令本身访问它。在我的版本中,为了简化它而杀死观察者,但是,上面的页面就是我正在处理的内容。 – omenclature

+0

你需要声明一个注入的$ rootScope服务,因为它们是在第294行,然后它可以作为参数传递给你的函数,如第276行所示。所以你将有IndexDirective。$ inject = ['$ rootScope'] ;然后函数IndexDirective($ rootScope)....也可以将rootScope从链接函数的参数中取出来,而不需要它。 –

+0

P.s. “vanilla”Angular不是这样的,我对elasticui不熟悉,但我确实认为它在语法上有点神秘。 –