Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax Angularjs使用前面的结果迭代$http rest调用_Ajax_Angularjs_Rest - Fatal编程技术网

Ajax Angularjs使用前面的结果迭代$http rest调用

Ajax Angularjs使用前面的结果迭代$http rest调用,ajax,angularjs,rest,Ajax,Angularjs,Rest,当在数组上迭代时调用$http资源时,locations数组会变得不一致,因为在locationService结果返回之前,i变量会被更新 homeService.get($scope.name).then(function (homes) { for (var i = 0; i < homes.length; i++) { locationService.get(homes[i]).then(function (result) { loca

当在数组上迭代时调用$http资源时,locations数组会变得不一致,因为在locationService结果返回之前,i变量会被更新

homeService.get($scope.name).then(function (homes) {
    for (var i = 0; i < homes.length; i++) {
        locationService.get(homes[i]).then(function (result) {
            locations.push(result);
        });
    }
});
homeService.get($scope.name)。然后(函数(homes){
对于(变量i=0;i

如何实现在数组上进行$http调用并在locationService结果之后更新I变量的迭代?因此,位置数组的更新是一致的。

您想保持位置数组的有序,以家为序吗?嗨,泉月,不,不是以家为序。问题是在迭代块中,它正在进行一个ajax调用,并且迭代在不等待ajax服务结果的情况下继续进行。一旦服务被调用,当它返回时,您的位置数组将被插入,即使迭代没有等待响应。您好,钱月,谢谢。在阅读了《你看不到问题》之后,我仔细梳理了我所有的服务,发现其中一项服务没有退货承诺,这意味着结果永远不会被设置和更新。