如何使用循环遍历角度 $scope 变量 [英] How to iterate through angular $scope variables with a loop

查看:25
本文介绍了如何使用循环遍历角度 $scope 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这样的 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 未定义 因为 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.

推荐答案

Angular 上面的做法是

Angular way to do above is

 $scope.accounts=[{name:"123"},{name:"124"},{name:"125"}]

            angular.forEach($scope.accounts,function(value,index){
                alert(value.name);
            })

这篇关于如何使用循环遍历角度 $scope 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆