给定一个指令(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
is undefined
因为objectExposedInContainer1
不在正确的范围内。
我有两个问题:
- Why
ng-transclude
如果指令具有隔离范围,则其范围不是其指令范围的子级?这是一个错误吗?
- 如果这不是一个错误,那么容器指令如何将数据传递到它的内容,如果不是像我尝试的那样设置属性。
这是一个不起作用的示例:http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview。由于 Plunker 是使用 Anguar 构建的,因此很难使用 Batarang 进行调试。我建议在本地下载代码。注释掉line 10
of app.js
使其使用共享范围工作。