2016-09-04 12 views
0

この場合、重大な問題があります。 以下のコードでgithub APIからデータを取得していますが、github alllowはページごとに30個の結果しか得られないため、より良いソートオプションを得るためにすべてのデータを取得し、オブジェクトの1つの配列にプッシュしたいと思います。以下は、この1つは複数のhttpコールの角度管理データ

$scope.getData = function() { 
     $http({ 
      method: "GET", 
      url: api url + pageNum 
     }).then(function mySucces(response) { 
      $scope.mydata = response.data; 
      $scope.isLoading = false; 
      $scope.result = $scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; }); 

     }, function myError(response) { 
      $scope.error = response.statusText; 

     }); 
    }; 

正常に動作します。しかし、私はすべてのアイデアは、HWこれを行うには、この

$scope.getData = function() { 
for(var i =0; i<= $scope.pages.length; i++){ 
     $http({ 
      method: "GET", 
      url: "apiUrl + i 
     }).then(function mySucces(response) { 
      $scope.mydata = response.data; 
      $scope.isLoading = false; 
      $scope.result = $scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; }); 

     }, function myError(response) { 
      $scope.error = response.statusText; 

     }); 
    }; 

ような何かをしたいコード を見つけますか?

+0

すべての約束事を解決するために$ q.all()を使って同様のことを行っています。残念ながら、私はreposに今アクセスすることはできませんし、私は例を試すために自宅で必要なセットアップを持っていません。その方向を見てみてください。 –

答えて

0

$q documentation in the AngularJS siteを確認する必要があります。

$scope.getData = function() { 
    $scope.result = []; 
    $scope.error = []; 
    $q.all($scope.pages.map(function(page) { 
     return $http({ 
      method: "GET", 
      url: "apiUrl" + i 
     }) 
     // This function will be triggered when each call is finished. 
     .then(function mySucces(response) { 
      $scope.mydata.push(response.data); 
      $scope.result.push($scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; })); 

     }) 
    })) 
    // This call will be called when all of the calls are finished. 
    .then(function(success) { 
     $scope.isLoading = false; 
    }).catch(function (error) { 
     $scope.error = response.statusText; 
    }); 
} 
+0

ありがとう、それをチェックするつもりです。知らせます – TrzasQ

関連する問題