给定一个具有 transclude 和隔离作用域的指令(container1
),当该指令被链接时,我有以下这些作用域:
Scope 004 <-- scope of the body
Scope 005 <-- scope of directive container1
Scope 006 <-- scope of the ng-transclude
我期望:Scope 004 <-- scope of the body
Scope 005 <-- scope of the directive
Scope 006 <-- scope of the ng-transclude
如果相同的指令具有共享作用域而不是隔离作用域,我会得到预期的结果。
这给我带来了问题,因为如果被转移的内容包含另一个具有隔离作用域的指令(component1
),我将得到:
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
我希望能够像这样使用指令:
<container1>
<component1 data="objectExposedInContainer1"/>
</container1>
但是在component1
内部,$scope.data
是undefined
,因为objectExposedInContainer1
不在正确的作用域内。
我有两个问题:
- 如果指令使用了隔离作用域,为什么
ng-transclude
的作用域不是其指令作用域的子级?这是一个错误吗? - 如果这不是错误,那么容器指令如何将数据传递给其内容,而不是像我尝试的设置属性。
这里有一个示例,它无法正常工作:http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview。由于Plunker是使用Angular构建的,所以很难使用Batarang进行调试。建议下载代码并注释掉app.js
的第10行
,以使用共享作用域使其正常工作。