下面是如何从父作用域创建子作用域的简化版本(作为示例)。要获得更全面的视图 - 请访问source
创建父范围:
function Scope(){ };
var scope = new Scope(); // scope instance is created
假设它有一些原始属性和一些对象属性:
scope.a = 1;
scope.obj = {a: 1};
创建子作用域实例,其原型为“scope”
function ChildScope(){ };
ChildScope.prototype = scope;
var childScope = new ChildScope(); // an instance of the child scope
现在,让我们看看:
console.log(scope.a); // should be 1
console.log(childScope.a); // should be 1
console.log(scope.obj.a); // should be 1
console.log(childScope.obj.a); // should be 1
正如预期的那样,对吗?
如果我们现在为暴露的属性设置值childScope
childScope.a = 2;
childScope.obj.a = 3;
然后是输出:
console.log(scope.a); // should still be 1
console.log(childScope.a); // should be 2 now
console.log(scope.obj.a); // should be 3
console.log(childScope.obj.a); // should be 3
jsFiddle
所以,当你在做的时候ng-repeat
在基元数组上:item in ["a", "b",...]
并在其中使用<input ng-model="item">
,你本质上是在做childScope.item = "..."
.