Access Parent Scope in Transcluded Directive

With transclude: true and scope: true, the parent directive creates two new scopes:
enter image description here

Scope 004 is a result of scope: true, and scope 005 is a result of transclude: true. Since the child directive does not create a new scope, it uses transcluded scope 005. As you can see from the diagram there is no path from scope 005 to scope 004 (except via private property $$prevSibling, which goes in the opposite direction of $$nextSibling — but don’t use those.)

@joakimbl’s solution is probably best here, although I think it is more common to define an API on the parent directive’s controller, rather than defining properties on this:

controller: function($scope) {
    $scope.SOME_CONST = 'someConst';
    this.getConst = function() {
       return $scope.SOME_CONST;
    }
}

Then in the child directive:

link:function(scope,element,attrs,parentCtrl){
    scope.SOME_CONST = parentCtrl.getConst();
},

This is how the tabs and pane directives work on Angular’s home page (“Create Components” example).

Leave a Comment