角度范围-隔离VS继承.哪一个更具限制性 [英] Angular scope - Isolated VS Inherited. Which one is more restrictive

查看:96
本文介绍了角度范围-隔离VS继承.哪一个更具限制性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几部您看过的视频并阅读了其他堆栈溢出线程,但仍然无法弄清楚哪个角度范围的限制更大.孤立或继承.从隔离的名称可以感觉到它是否是限制性最强的范围,但由于它允许@,=和&等各种设置,对我来说,似乎没有那么限制,而是继承了范围.

I have seen several you tube videos and read other stack overflow threads but still cannot figure out which one of angular scope is more restrictive. Isolated or Inherited. From the name isolated it feels if it is the most restrictive scope but since it allows various settings like @, =, and & to me it seems less restrictive then inherited scope.

所以问题是哪一个限制更严格,为什么呢?

So the question is which one is more restrictive and why ?

推荐答案

我敢猜测,您对限制性"的定义与访问外部范围的数据有关.

I'd venture a guess that your definition of "restrictive" has to do with access to data in the outer scope.

使用该定义,隔离更加严格.隔离范围不会从其父级继承,因此它无权访问在其父级上定义的变量. (您仍然可以通过scope.$parent.p访问它们).

With that definition, isolated is more restrictive. The isolate scope does not inherit from its parent and so it does not have access to variables defined on its parent. (you could still access them via scope.$parent.p).

继承的作用域scope: true,创建从父项继承的子作用域,因此它可以访问父作用域上公开的所有变量.

Inherited scope scope: true, creates a child scope that inherits from the parent, and so it has access to all the variables exposed on the parent scope.

因此,如果您具有以下条件

So, if you have the following

<div ng-init="p1 = 'foo'; p2 = 'bar'">
  <isolate p="p1"></isolate>
  <inherited></inherited>
</div>

和定义为的指令:

.directive("isolate", function(){
  return {
     scope: {
        p: "="
     },
     link: function(scope){
        var p1a = scope.p;  // "foo"
        var p1b = scope.p1; // undefined
        var p2  = scope.p2; // undefined
     }
  }
})
.directive("inherited", function(){
  return {
     scope: true,
     link: function(scope){
        var p1 = scope.p1; // "foo"
        var p2 = scope.p2; // "bar"
     }
  }
}

isolate指令将仅看到scope.p" - which is a proxy for p1 , and继承的`将看到"

isolate directive will only see scope.p" - which is a proxy forp1, andinherited` will "see"

这篇关于角度范围-隔离VS继承.哪一个更具限制性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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