AngularJS Transcluded指令的范围来自其他指令

问题描述 投票:0回答:1

我得到了一个被转移的指令,例如'aDirective'和其他随机'bDirective'指令。我的任务是:我希望得到一个'aDirective的范围变量并在'bDirective'中捕获它。

angular.module('myApp',[])
  .controller('bDirective',['$scope',function(scope){

    scope.getScopeVar = function () {

    // here I want to get aDirective - s 'someVar' variable
    scope.someVar;

        debugger;
    };
    scope.getScopeVar();
  }])
  .directive('aDirective',function(){
    return{
    scope:{},
    transclude:true,
    template:'<div>123</div>',
    link: function(scope, element, attrs, controller, transclude){
      scope.someVar = 'asd';

      transclude(scope, function (clone) {
        element.append(clone);
      });
    }
    };
});

有解决方案吗关心尼克。

angularjs angularjs-directive scope angularjs-ng-transclude
1个回答
0
投票

嵌套指令应该是require the directive from the top。然后它可以接收它的控制器作为link函数参数(第4个)。

.directive('nestedDirective', function(){
 return {
   require: '^aDirective',
   link: function (scope, elements, attrs, aDirectiveController) {
     // access aDirectiveController's methods or properties
   }
 }
})
© www.soinside.com 2019 - 2024. All rights reserved.