如何使用循环遍历角度$ scope变量 [英] How to iterate through angular $scope variables with a loop
问题描述
我想用这样的for循环遍历$ scope变量. 在此示例中,$ scope对象包含一个对象 accounts 包括5个对象,它们的名称是从1到5的数字.每个对象都有一个名称.
I want to iterate through $scope variables with a for loop like this. In this example the $scope object includes an object accounts inlcuding 5 objects, whose names are numbers from 1 to 5. Each of them has a name.
for(var i = 1; i < 5; i++){
$('#name').val($scope.accounts.i.name);
}
问题: $ scope.accounts.i 是未定义,因为我不认为 $ scope内有杂物变量. 它算作字母i,因此我看不到有机会遍历带for循环的作用域. 当我在$ scope变量周围使用"时,它将仅显示为纯html,并且不会解释angular.
The problem: $scope.accounts.i is undefined because i does not count as a varibale inside the $scope variable. It counts as the letter i, so I see no chance to iterate through a scope with a for loop. When I use " " around the $scope variable it will just be displayed as plain html and angular is not interpreted.
推荐答案
上面的方法是
$scope.accounts=[{name:"123"},{name:"124"},{name:"125"}]
angular.forEach($scope.accounts,function(value,index){
alert(value.name);
})
这篇关于如何使用循环遍历角度$ scope变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!