假设您有:
obj : {
first: {
second: [1, 2, 3]
}
}
尝试这样做:
obj.nonexistent.prop // Nested property on nonexistent parent
obj.first.second[1000] // Out of bound array access
会在javascript中抛出错误,但Angular不会抛出错误
对于别名变量作为处理变量阴影的一种方法,请设想如下:
<div ng-controller="ItemsController">
<div ng-repeat="item in items" ng-init="outerCount = $index">
{{outerCount + 1}}. {{item.name}}
<div ng-repeat="item in item.items">
{{outerCount + 1}}.{{$index + 1}}. {{item.name}}
</div>
</div>
</div>
从…起
here
在ng重复中
$index
变量每次迭代都会更改为指向循环的当前索引。因此,如果有嵌套循环,则会丢失对外部循环的引用
$索引
变量这是
可变阴影
。要使用外部变量,可以使用
ng-init
并将其设置为
$索引
外部现在,你已经
混叠的
外部
$索引
变量为
outerCount
.
希望这是清楚的!