简体   繁体   English

为什么会导致继承?

[英]Why does this result in inheritance?

I was reading this article on scope inheritance in AngularJS and was confused by this code example: 我正在阅读有关AngularJS中的作用域继承的文章 ,并对以下代码示例感到困惑:

angular.module("Demo", [])  
  .controller("ChildCtrl", function($rootScope, $scope) {
    $rootScope.rootyThing = "I am groot";
    console.log($scope.rootyThing);  // "I am groot"
    console.log(Object.getPrototypeOf($scope)); // Scope
 });

I don't understand why $scope.rootyThing is set instead of undefined . 我不明白为什么设置$scope.rootyThing而不是undefined

The article's explanation seems incomplete. 文章的说明似乎不完整。 The fact that the child scope "prototypically inherits" from $rootScope would not explain this, seeing as rootyThing is not set on the prototype, and moreover was set after the creation of the child scope $scope . 子范围从$rootScope “原型继承”的事实并不能解释这一点,因为rootyThing不在原型上设置,而且是在子范围$scope创建之后设置的。

The only explanation is if the scopes in Angular are deeply modified such that all variables set on them are broadcast to existing child scopes. 唯一的解释是,如果对Angular中的范围进行了深度修改,以使在它们上设置的所有变量都广播到现有的子范围。 Unless I'm missing something, more than possible. 除非我缺少任何东西,否则可能会更多。

Can anyone explain this? 谁能解释一下?

Edit: My current understanding is that $rootScope is in fact the Scope function itself rather than an instance of Scope , and all $scope instances use this as a root prototype, so when variables are set on the function Scope then they're naturally accessible to the various $scope instances. 编辑:我现在的理解是, $rootScope实际上Scope功能本身,而不是实例 Scope ,所有$scope情况下,以此为根原型,所以当变量的函数设定Scope那么他们自然可以访问到各种$scope实例。

Is this accurate? 这个准确吗?

All scopes are added on $rootScope object. 所有作用域都添加到$rootScope对象上。 If you add a property(for example someProperty)on $rootScope and you try to access it using $scope.someProperty , then it will be checked that this property exists on $scope (ie current scope). 如果在$rootScope上添加一个属性(例如someProperty),并尝试使用$scope.someProperty访问它,则将检查该属性在$scope上是否存在(即当前范围)。 If that property does not exist, then it will be checked on higher level in scope chain(ie $rootScope ). 如果该属性不存在,则将在作用域链的更高级别(即$rootScope )对其进行检查。

ng-controller will create a new Scope . ng-controller将创建一个new Scope

this scope's prototype is set to parent Scope (ie, $rootScope in this case) 该作用域的原型设置parent Scope (即本例中的$rootScope

And it's the default javascript behavior to look in the prototype chain if the property which we are looking for is not found in the object. 如果在对象中找不到我们要查找的属性,那么这是默认的JavaScript行为,它会在原型链中进行查找。

范围继承

It is set to the prototypes, try console.log 它已设置为原型,请尝试console.log

$scope.__proto__.rootyThing

And you should see it there. 而且您应该在那里看到它。

Furthermore, objects are by reference in javascript so it doesnt matter when $scope was set for example 此外,对象是通过javascript中的引用进行的,因此例如在设置$ scope时就没有关系

//say this is your rootScope
objRoot = {
   obj: {
       test: 'hello'
   }
}

//Now lets create a scope
var temp = objRoot.obj

//Update rootScope
objRoot.obj.test = "changed"

//log your temp
console.log(temp.test); //changed

In AngularJs, as far as I know, the scopes are inherited from the parent scope, all the variables , but if u have a sibling scope then those values will not be inherited. 据我所知,在AngularJs中,作用域是从父作用域( 所有变量)继承 ,但是如果u有同级作用域,则这些值将不会被继承。 The broadcasting is done for events. 广播是为事件完成的。

So, Angular is working as it has to be. 因此,Angular正在按需工作。 If you have set some variable on the $rootScope that will be accesible throughout the App. 如果您在$ rootScope上设置了一些变量,则该变量在整个App中均可使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM