我正在使用angularjs和ionic开发应用程序。 我有一个带有ID的数组。从所有这些ID中我需要有一个名字。现在我尝试使用下面的代码:
var arrayWithIds = [1, 2, 3, 4, 5, 6, 7]
var arrayWithNames = [];
for (var j = 0; j < arrayWithIds.length; j++) {
ResourceService.get(arrayWithIds[j]).then(function(resource) {
arrayWithNames.push(resource.Name);
},function(error) {
alert(error.message);
});
}
$scope.resources = arrayWithNames;
调试时一切正常。我总是得到这个名字。但是在$ scope.resources中没有什么,它是空的,也是数组arrayWithNames。
我错过了什么吗?有什么问题?
感谢。