因为ng-repeat
创建了一个新的范围。
这已经回答了很多次,因为细微差别有点难以理解,特别是如果你不了解 js 原型继承的一切:https ://github.com/angular/angular.js/wiki/Understanding-Scopes
编辑:看来这个答案很有争议。需要明确一点——这就是 JS 的工作方式。在了解 JS 的工作原理之前,您真的不应该尝试学习 Angular。但是,链接似乎确实错过了
因此,这里有一个关于 JS 在这种情况下如何工作的示例:
var a = {value: 5};
var b = Object.create(a); // create an object that has `a` as its prototype
// we can access `value` through JS' the prototype chain
alert(b.value); // prints 5
// however, we can't *change* that value, because assignment is always on the designated object
b.value = 10;
alert(b.value); // this will print 10...
alert(a.value); // ... but this will print 5!
那么,我们该如何解决呢?
好吧,我们可以“强迫”自己通过继承链——因此我们将确保我们总是访问正确的对象,无论是访问值还是修改它。
var a = {obj: {value: 5}};
var b = Object.create(a); // create an object that has `a` as its prototype
// we can access `value` through JS' the prototype chain:
alert(b.obj.value); // prints 5
// and if we need to change it,
// we'll just go through the prototype chain again:
b.obj.value = 10;
// and actually refer to the same object!
alert(b.obj.value == a.obj.value); // this will print true