指令内的内容与隔离和非隔离示波器 [英] Directive inner content with isolated and non-isolated scopes

查看:183
本文介绍了指令内的内容与隔离和非隔离示波器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的范围相当奇怪的现象:真正的类型指令的内部内容:

I found quite a strange behaviour of scope:true type for inner content of a directive:

<body ng-init="x=10">
    <mydir>
       {{x}}
    </mydir> 
</body>

所以{{X}}是内部内容,并指令的定义是:

so {{x}} is inner content, and directive definition is:

.directive('mydir', function() {
   return {
       scope: {},
       link: function(scope){
           scope.x = 5;
       }
   };
});

当我们定义的范围是孤立的(范围:{} ),输出{{X}}为10,所以使用外部范围。但是,当我们的指令创建新的范围(范围:真正的),它会用它内在的内容和输出5.因此它使用了不同的范围为2例内的内容。可能有人给我一个提示/链接为周守军这种不一致的解释code /手册?

When we define scope as isolated (scope:{}), it outputs {{x}} as 10, so uses outer scope. But when we create new scope for the directive(scope:true), it will use it for inner content and output 5. So it uses different scopes for inner content for 2 cases. Could somebody give me a hint/link to souce code/manual for the explanation for this inconsistency?

下面是 plnk 与code玩。

Here is plnk to play with the code.

UPD :我明白了什么是JavaScript的原型继承。我知道指令范围类型之间的差异。与我的目标是不显示5而不是10的问题是关于,在这两种情况下,应与父范围,不具有访问儿童/孤立一个属性内插的内模板

UPD: I understand what is JavaScript prototype inheritance. I know the difference between directive scope types. And my aim is not to display 5 instead of 10. The question is about inner template that in both cases should be interpolated with parent scope, which does not have access to properties of child/isolated one.

推荐答案

一个得到了角的问题的答案:
https://github.com/angular/angular.js/issues/13845 #issuecomment-174953398

A got the answer from angular issues: https://github.com/angular/angular.js/issues/13845#issuecomment-174953398

这是源$ C ​​$ C答案:
<一href=\"https://github.com/angular/angular.js/blob/eae0a1121ffcc636d760463105dcdc548ea47390/src/ng/compile.js#L2538-L2545\" rel=\"nofollow\">https://github.com/angular/angular.js/blob/eae0a1121ffcc636d760463105dcdc548ea47390/src/ng/compile.js#L2538-L2545

And here is source code answer: https://github.com/angular/angular.js/blob/eae0a1121ffcc636d760463105dcdc548ea47390/src/ng/compile.js#L2538-L2545

var scopeToChild = scope;
if (newIsolateScopeDirective && (newIsolateScopeDirective.template || newIsolateScopeDirective.templateUrl === null)) {
      scopeToChild = isolateScope;
}
childLinkFn && childLinkFn(scopeToChild, linkNode.childNodes, undefined, boundTranscludeFn);

简历:
在隔离范围类型的情况下,的范围仅适用于指令模板作为内提供的内容,但不是。

resume: In case of isolated scope type, scope is provided ONLY for directive template, but not for inner content.

这篇关于指令内的内容与隔离和非隔离示波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆