[英]Angular JS how to update scope variable inside another scope variable
[英]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;
}
請參見: 使用變量動態訪問對象屬性
另外,如果可以改為使用數組,則應避免以這種方式訪問屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.