0

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.

KoboldMines
  • 428
  • 2
  • 6
  • 21

1 Answers1

1

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
   }
 }
})
georgeawg
  • 48,608
  • 13
  • 72
  • 95
fracz
  • 20,536
  • 18
  • 103
  • 149
  • Thanks for your answer. Is there any way of making this without **require: '^aDirective',**? Because, that **transclude: true** Directive is global directive and I can not require only one particular Directive from it. – KoboldMines Oct 29 '18 at 09:19
  • 1
    You `require` directive in the directive that is nested (transcluded), not the one that declares transclusion with `transclude: true`. – fracz Oct 30 '18 at 06:44