Angular JS 教程中的混淆术语
Confuse terms in Angular JS tutorial
我正在学习教程,但我很难理解某个句子,该句子谈到在 Angular 中命名我的属性时可能出现的问题:
It quietly tolerates all property access errors, including nested properties on nonexistent parents and out-of-bounds array access.
aliasing variables as a way to cope with variable shadowing
我的英文还好,能看懂写的是什么,但是"nested properties on nonexistent parents"和"aliasing variables as a way to cope with variable shadowing"是什么我真的看不懂。查了一下,没看明白
谁能给我一个明确的解释?
假设你有:
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-repeat 中,$index
变量在每次迭代时更改为指向循环的当前索引。因此,如果您有嵌套循环,您将丢失对外部 $index
变量的引用。这是变量阴影。要使用外部变量,可以使用ng-init
并将其设置为外部的$index
。现在,您已将外部 $index
变量 别名 为 outerCount
。
希望这是清楚的!
我正在学习教程,但我很难理解某个句子,该句子谈到在 Angular 中命名我的属性时可能出现的问题:
It quietly tolerates all property access errors, including nested properties on nonexistent parents and out-of-bounds array access.
aliasing variables as a way to cope with variable shadowing
我的英文还好,能看懂写的是什么,但是"nested properties on nonexistent parents"和"aliasing variables as a way to cope with variable shadowing"是什么我真的看不懂。查了一下,没看明白
谁能给我一个明确的解释?
假设你有:
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-repeat 中,$index
变量在每次迭代时更改为指向循环的当前索引。因此,如果您有嵌套循环,您将丢失对外部 $index
变量的引用。这是变量阴影。要使用外部变量,可以使用ng-init
并将其设置为外部的$index
。现在,您已将外部 $index
变量 别名 为 outerCount
。
希望这是清楚的!