使用$ q.所有这些都是为了让同步HTTP调用无法正常工作
using $q.all for making synchronous http calls not working angularjs
我遇到了以下问题,无法继续。我尝试了一些解决方案,张贴在不同的问题,但无法得到它的工作
在我的控制器,我有一个$scope.init()函数。我有一个for循环在它调用一个函数来制作http。获取对不同url的调用,每个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);
};
如何解决这个问题?
这样写怎么样?
http://plnkr.co/edit/pjWbNX1lnE2HtaNs1nEX?p =
预览$scope.init = function ( ){
for (var i=0; i < 10; i++) {
$scope.getNextList(i) // push calls into array
};
var index = 0;
function makeCall() {
$scope.asyncCall[index]
.success(function(data) {
if (index < $scope.asyncCall.length - 1) {
console.log(index);
index += 1;
makeCall();
}
else {
console.log(index); // last call
}
})
}
makeCall();
};
相关文章:
- 为什么ng控制器不调用或工作或功能不工作
- Javascript作用域和Ajax调用;工作不正常
- 给出<选项>标记一个类?API调用不工作
- 让jquery插件在Ajax调用后工作
- 按钮上的数据加载消息无法与ajax调用一起工作
- 当控制台不在't在chrome中打开,有些项目不再可点击(有些js调用停止工作)
- 如何使pjax:postate像一个简单的pjax调用一样工作
- REST API 调用仅工作一次
- 如何确保 javascript 函数在多个调用上工作
- AJAX 调用只工作一次
- Rails 4 Ajax 调用只工作一次
- 对单个 PHP 脚本的多个 AJAX 调用划分工作负载 = 服务器端多线程
- AJAX 调用只工作一次
- 异步服务调用不工作
- jquery:制作按钮click&json调用一起工作,但保持它们分离
- AJAX GET调用将在第一次调用时工作,但在其他单击之后不工作
- AJAX调用不工作在Cordova 3.5,但将工作时,在浏览器中查看
- Ajax调用不工作与onclick
- Ajax调用有时工作,有时工作和刷新,有时刷新和失败....
- 封装的API.对外部javascript函数的调用不工作