AngularJS Transcluded指令的范围与其他指令的范围有关

时间:2018-10-29 07:52:54

标签: angularjs angularjs-directive scope angularjs-ng-transclude

我得到了包含在内的指令,例如“ 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);
      });
    }
    };
});

有解决方案吗? 问候尼克。

1 个答案:

答案 0 :(得分:0)

嵌套指令应为require the directive from the top。然后,它可以将其控制器作为link函数参数(第四个参数)来接收。

.directive('nestedDirective', function(){
 return {
   require: '^aDirective',
   link: function (scope, elements, attrs, aDirectiveController) {
     // access aDirectiveController's methods or properties
   }
 }
})