我已经知道transclusion是如何工作的(仅在第一级我猜),但我有一个关于嵌套transcluded项目的范围的问题。嵌套 - 透明物品 - 范围澄清?
好了,所以我有这样的代码:
<body ng-app="docsTabsExample" ng-controller="ctrl">
<my-tabs>
<my-pane title="Hello">
<h4>Hello , The value of "i" is => {{i}}</h4>
</my-pane>
</my-tabs>
</body>
基本上我有一个controller
,<my-tabs>
和<my-pane >
。在myTabs
指令
展望:
.directive('myTabs', function()
{
return {
restrict: 'E',
transclude: true,
scope:
{},
controller: ['$scope', function($scope)
{
$scope.i = 2;
}],
template: '<div ng-transclude></div>'
};
})
我知道指令的内容将有机会获得外指令的范围
所以黄色部分将有机会获得外范围(这是主控制器范围):
这里是myPane
指令代码:
.controller('ctrl', function($scope)
{
$scope.i = 1000;
})
程序的输出是:
.directive('myPane', function()
{
return {
require: '^myTabs',
restrict: 'E',
transclude: true,
scope:
{
},
controller: function($scope)
{
$scope.i = 4; //different value
},
template: '<div ng-transclude></div>'
};
})
该程序将启动
你好,的“我的价值“is => 1000
但
根据文档:myPane's
transcluded数据应该可以访问哪些是myTabs
指令,它的值为i=2
指令的外部范围。
但myPane
有孤立范围,以便它不继承myTabs
范围。
问题
所以它再涨到控制器的范围多个等级为了得到i=1000
? (澄清,我不问我怎么能让i
获得另一个价值 - 我问为什么/如何具有1000的价值)。
我的意思是范围层次结构在这里看起来如何?
这是这样吗?
controller's scope
|
+--------+---------+
| |
myTabs's mypanes's
transcluded transcluded
data's scope data's scope
的文档说:
的transclude选项更改范围嵌套的方式。它使得它 ,以便transcluded指令的内容有,无论范围是 指令以外,而不是内部的任何范围。在 这样做时,它将内容访问到外部范围。
但myPAne
指令的外部有什么范围?
换句话说,为什么/ 如何确实i=1000
?
编辑从OP的答案后
安装和配置潜望镜(从@MarkRajcok)后,现在我可以看到它在视觉上:
两个MYTAB和myPane有'transclude:TRUE'。 myPane嵌套在myTab中。你给myPane myTab的范围,然后myTab是ctrl的范围,这给了myPane ctrl的范围。 – jperezov