从角度范围获取控制器

时间:2015-06-04 06:32:50

标签: angularjs

我有像这样的指令结构

Parent
 |-----Wrapper---|
                 |----Direct 1
                 |----Direct 2
                 |----Direct 3
                 |----Direct 4

我想访问Directive 3具有的某些值。我的计划是使用Parent上的共享范围,然后像这样遍历孩子 -

.directive('readerDirective',['$timeout',function($timeout){
         return {
           restrict : 'A',
           link:function(scope, elm, attrs){
             var elemDire = elm.find('[Direct3]');

             var direScope = angular.element(elemDire).scope();

             direScope.$watch('value',function(){
               console.log(direScope.value);
             });
           }
         };
       }])

但我无法超越Wrapper。我可以访问Wrapper范围,但无法访问子指令。

我想访问Direct 3的控制器。有可能吗?

我无法控制Wrapper及其子项作为极端角度插件。

2 个答案:

答案 0 :(得分:0)

我发现您不太可能无法修改/覆盖Wrapper,但是您是否考虑过仅使用$rootScope来分享您的值?

或者,如果您可以找到DOM元素,则可以通过查找范围 angular.element(ELEMENT_SELECTOR).scope()

答案 1 :(得分:0)

我想我发现了这个问题。很抱歉回答我自己的问题,但它可能对某人有帮助。

我的readerDirective被执行后,DOM正在渲染。有了这个,我无法找到元素。

解决方案是使用

$timeout(function(){
},0)

这样的事情有效:

.directive('readerDirective',['$timeout',function($timeout){
         return {
           restrict : 'A',
           link:function(scope, elm, attrs){

         $timeout(function(){
            var elemDire = elm.find('[Direct3]');

            var direScope = angular.element(elemDire).scope();

            direScope.$watch('value',function(){
               console.log(direScope.value);
            });
         },0);

       }
     };
   }])
相关问题