Given a directive (container1
) with transclude and an isolated scope, when the directive is linked then I have these scopes:
Scope 004 <-- scope of the body
Scope 005 <-- scope of directive container1
Scope 006 <-- scope of the ng-transclude
I expected:
Scope 004 <-- scope of the body
Scope 005 <-- scope of the directive
Scope 006 <-- scope of the ng-transclude
If the same directive has a shared scope instead of an isolated scope, I get the expected result.
This causes me a problem because, if the transcluded content contains another directive (component1
) with an isolated scope, I get:
Scope 004 <-- scope of the body
Scope 005 <-- scope of the directive
Scope 006 <-- scope of the ng-transclude
Scope 007 <-- scope of directive component1
I want to use the directives like this:
<container1>
<component1 data="objectExposedInContainer1"/>
</container1>
But that does not work, inside component1
, $scope.data
is undefined
because objectExposedInContainer1
is not on the right scope.
I have two questions:
- Why
ng-transclude
's scope is not a child of its directive's scope if the directive has an isolated scope? Is this a bug?
- If it's not a bug, how can a container directive pass data to it's content, if not by setting attributes like I tried.
Here is a sample where it does not work: http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview. Because Plunker is built with Anguar, it's hard to debug with Batarang. I recommend downloading the code locally. Comment out line 10
of app.js
to make it work using a shared scope.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…