angular 更新循环内的 $scope 变量
angular update $scope variable inside loop
我正在尝试更新 $scope 变量:
例如:
$scope.variable_1
$scope.variable_2
...
我想这样更新:
for (i=0; i<2; i++) {
$scope.variable_$i = 1;
}
我需要的是在每次迭代中使用索引 "i" 访问“$scope.variable_1”。
有什么建议吗?提前致谢。
在 javascript 中,您可以通过名称访问变量:
for (i=0; i<2; i++) {
$scope['variable_'+i] = 1;
}
参见:Dynamically access object property using variable
此外,如果您可以使用数组代替,则应避免以这种方式访问属性。
我正在尝试更新 $scope 变量:
例如:
$scope.variable_1
$scope.variable_2
...
我想这样更新:
for (i=0; i<2; i++) {
$scope.variable_$i = 1;
}
我需要的是在每次迭代中使用索引 "i" 访问“$scope.variable_1”。
有什么建议吗?提前致谢。
在 javascript 中,您可以通过名称访问变量:
for (i=0; i<2; i++) {
$scope['variable_'+i] = 1;
}
参见:Dynamically access object property using variable
此外,如果您可以使用数组代替,则应避免以这种方式访问属性。