【发布时间】:2015-07-09 05:15:32
【问题描述】:
我遇到了以下问题,无法继续。我尝试了一些在不同问题中发布的解决方案,但无法正常工作
在我的控制器中,我有一个 $scope.init() 函数。我有一个 for 循环,它调用一个函数来对不同的 url 进行 http.get 调用,每个 url 取决于前一个调用的数据,所以我需要它是同步的
$scope.init = function() {
decodedURL = $routeParams.url;
//evaluate some variables, ampIndex is > -1 here
for( var i=0; ampIndex > -1; ++i)
{
decodedURL = decodedURL.substring(ampIndex+1, decodedURL.length);
ampIndex = decodedURL.indexOf("&");
$scope.getNextList(i);
/* above function call makes the http.get call to the currentURL based on
decodedURL, and the data is stored in variable[i+1], so for the next
iteration, the calls should be synchronous
*/
$q.all(asyncCall).then(function (data) {var j;} );
/* I wrote the above dummy statement so that it is executed only after
http.get in $scope.getNextList() function is successful, but it is
not working
*/
}
};
$scope.getNextList = function(index) {
// $currentURL is calculated
var hello = _helpers.server.http($http, $scope.currentURL) {
.success( function(response) {
})
.error( fucntion(errResponse) {
});
asyncCall.push(hello);
};
我该如何解决这个问题?
【问题讨论】:
-
所以问题是调用不是同步的?可能是因为 $q.all 在 for 循环内...
-
是的,调用不是同步的.....我把 $q 放在里面是因为在每次迭代中,都有一个 http 调用,应该是同步的
-
如果您的 asyncCall 仅包含 1 个 promise,为什么还要使用 $q.all ?或者您可能应该刷新您的 asyncCall 以仅包含 1 个承诺...
-
在每次迭代中,一个promise被压入asyncCall,是不是不对?
-
您正在寻找顺序执行,而不是同步执行。
标签: javascript angularjs http get