angularjsangularjs-directivescopeangularjs-ng-transclude

AngularJS Transcluded directive's scope catch from other Directive


I got transcluded directive, for example 'aDirective' and other random 'bDirective' directive. My task is: I want to get a 'aDirective's scope variable and catch it in '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);
      });
    }
    };
});

Any solutions? Regards Nick.


Solution

  • The nested directive should require the directive from the top. Then it can receive its controller as a link function argument (the 4th one).

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