簡體   English   中英

使用$ q.all進行同步http調用不起作用angularjs

[英]using $q.all for making synchronous http calls not working angularjs

我遇到以下問題,無法繼續。 我嘗試了在不同問題中發布的一些解決方案,但無法使其正常工作

在我的控制器中,我有一個$ 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);
};

我該如何解決這個問題?

沿着這些思路怎么樣?

http://plnkr.co/edit/pjWbNX1lnE2HtaNs1nEX?p=preview

$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();
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM