【发布时间】:2013-11-01 15:02:51
【问题描述】:
我正在尝试使用指令进行模板化,但不幸的是,每个指令似乎都为模板和控制器/链接功能设置了单独的范围。
plnkr 的示例:
<body ng-app="App">
<h2>Directive with Isolating Scope</h2>
<isolating some-value="isolated">{{someValue}}</isolating>
<h2>Directive with Shared Scope</h2>
<sharing some-value="shared">{{someValue}}</sharing>
</body>
var app = angular.module('App', []);
app.directive('isolating', function(){
return {
'restrict': 'E',
'scope': {
'someValue': '@'
},
'transclude': true,
'template': '<div ng-transclude></div>',
'link': function(scope, element, attrs){
scope.someValue = attrs.someValue;
}
};
});
app.directive('sharing', function(){
return {
'restrict': 'E',
'transclude': true,
'template': '<div ng-transclude></div>',
'link': function(scope, element, attrs){
scope.someValue = attrs.someValue;
}
};
});
我在 Batarang 中看到的内容:(括号中的指令名称)
< Scope (002)
< Scope (003) <= (isolating) contains the isolated scope
< Scope (004) <= (isolating) contains the template scope
< Scope (005) <= (sharing) contains the shared scope
如何将隔离范围 003 用于模板? Scope 004 似乎完全没有必要。
AngularJS 版本是 1.2.0-rc3。
【问题讨论】:
-
好吧,看来我真的简化了我的例子太多了。在我的应用程序中,我嵌套了多个类似于上述指令的指令,并且希望在从同一指令添加新变量的同时传递父作用域的一部分,因为它们需要在更深的嵌套级别上。所以我真的需要将隔离的控制器/链接范围传递给被嵌入的部分并从那里访问它。
标签: javascript angularjs