我在angularJS中有一些这样的代码:
$scope.myArray= [{name:"thomas", parentId: 1}, {name:"john", parentId: 2}]
for(var i = 0; i< $scope.myArray.length, i++){
// I wish this code run as blocking code
$http.get('getParentById'+$scope.myArray[i].parentId).then(function(result){
if(result){
$scope.myArray[i].parentName = result.data;
}
});
}
我的意图很清楚,为通过Http请求获得的每个对象分配parentName。但是问题是在angularJs中是异步的。我尝试了很多方法,但无法解决。有人对此有解决方案吗?
编辑:我在下面的评论中得到了答案。解决方案是使用angular.foreach()