給定一個指令(container1
)與transclude和隔離範圍,在該指令被鏈接然後我有這些範圍:爲什麼ng-transclude的範圍不是其指令範圍的子節點 - 如果指令具有隔離範圍?
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
如果同一指令有一個共享範圍而不是隔離範圍,我得到了預期的結果。
這使我一個問題,因爲,如果transcluded內容包含另一個指令(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
的範圍不是其指令的範圍的孩子,如果該指令具有隔離範圍是什麼?這是一個錯誤? - 如果不是bug,容器指令如何通過設置像我嘗試的屬性來傳遞數據到它的內容。
下面是一個它不起作用的示例:http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview。由於Plunker與Anguar一起構建,因此很難用Batarang進行調試。我建議在本地下載代碼。使用共享範圍註釋掉app.js
的line 10
以使其工作。
1)不,你有你問什麼了 - 該指令的範圍是孤立的。 2)使用共享控制器。 –
@Joe Gauterin,看看這個新樣本:http://plnkr.co/edit/Bv7B4OokkLi8bIctCIl3。這裏'container1'包含'component1',但不使用'ng-transclude'。這一次,即使兩者都有獨立的範圍,它們的範圍具有正確的父母/子女關係。 「ng-transclude」的存在改變了結果。 – Sylvain