【发布时间】:2016-05-19 20:52:49
【问题描述】:
继续讨论 Confused about Angularjs transcluded and isolate scopes & bindings
<controller>
<directive>
transcluded html
</directive>
</controller>
通过应用程序中的上述一般结构,在链接讨论中暗示,嵌入范围基本上是从父范围(控制器)继承的,并且嵌入范围不能访问指令范围。解释这个的文章是http://angular-tips.com/blog/2014/03/transclusion-and-scopes/
我的问题是 - 嵌入范围是否可以访问指令范围?
根据上面的文章,如果我们在指令的链接函数中使用transclude函数并将代码编写为:
transclude(scope, function(clone, scope) {
element.append(clone);
});
这真的有效吗?我在我的应用程序上尝试了同样的方法,但它不起作用。这是我一直在使用的代码。
指令定义:
(function(){
'use strict';
angular.module('checkoutApp')
.directive('wizardCard', ['shipToService','deliveryService','billingService', wizardCardDirective]);
function wizardCardDirective(shipToService,deliveryService,billingService){
return {
restrict : 'E',
scope : {
current : '@',
checkoutStates: '='
},
transclude:true,
templateUrl: '/UsabilitySites/Cart/Checkout/app/components/shared/wizard-card.html',
link: function(scope, element, attrs,ctrl,transclude){
scope.bla == "ajcnasc";
transclude(scope, function(clone, scope) {
element.append(clone);
});
}
};
}
})();
wizard-card.html -
<div class="wizardContainer">
{{bla}}
</div>
范围变量范围在打开 html 时为空。 谁能告诉我为什么会这样?
上述问题已解决 更新新问题:
现在我尝试使用多槽嵌入来执行此操作,但它不起作用。
指令定义:
(function(){
'use strict';
angular.module('checkoutApp')
.directive('wizardCard', [wizardCardDirective]);
function wizardCardDirective(){
return {
restrict : 'E',
scope : {
current : '@',
checkoutStates: '='
},
transclude: {
'completed': 'completed',
'inprogress': 'inprogress'
},
templateUrl: 'wizard-card.html',
link: function(scope, element, attrs,ctrl,transclude){
scope.bla = "ajcnasc";
transclude(scope, function(clone, scope) {
element.append(clone);
});
}
};
}
})();
wizard-card.html -
<div class="wizardContainer">
<div ng-transclude="completed">
</div>
<div ng-transclude="inprogress">
</div>
</div>
正在使用的指令 -
<wizard-card current="shipping" checkout-states="checkoutStates">
<completed>
bla {{bla}}
</completed>
<inprogress>
{{bla}}
</inprogress>
</wizard-card>
这也给了我一个空白,而 scope.$id 给了我另一个值(与指令不同)。
根据概念,它应该以与多槽嵌入相同的方式工作。但我不知道为什么它不能使用这段代码。
【问题讨论】:
-
出于调试目的,您可以将
{{$id}}放入您的模板中。如果$interpolation工作正常,这将暴露范围 ID。 -
您错误地使用了 double 等于
==。而是做scope.bla = "ajcnasc"。 DEMO on JSFiddle.
标签: angularjs angularjs-directive angularjs-ng-transclude transclusion