我正在浏览AngularJS
的forEach
loop
。关于它,有几点我是不理解的。
angular.forEach($scope.data, function(value, key){});
PS:我试图在没有参数的情况下运行这个函数,但它不起作用。
这是我的json
[
{
"Name": "Thomas",
"Password": "thomasTheKing"
},
{
"Name": "Linda",
"Password": "lindatheQueen"
}
]
我的JavaScript
文件:
var app = angular.module('testModule', []);
app.controller('testController', function($scope, $http){
$http.get('Data/info.json').then(
function(data){
$scope.data = data;
}
);
angular.forEach($scope.data, function(value, key){
if(value.Password == "thomasTheKing")
console.log("username is thomas");
});
});
另一个问题:为什么上面的函数没有进入if条件,并在控制台打印"username is thomas“?
发布于 2016-09-09 15:57:08
你必须为JSON使用嵌套的angular.forEach循环,如下所示:
var values = [
{
"name":"Thomas",
"password":"thomas"
},
{
"name":"linda",
"password":"linda"
}];
angular.forEach(values,function(value,key){
angular.forEach(value,function(v1,k1){//this is nested angular.forEach loop
console.log(k1+":"+v1);
});
});
https://stackoverflow.com/questions/29953198
复制相似问题