2017-01-12 4 views
0

申し訳ありませんが、私は答えをチェックし、まだそれを作る方法を理解しないいる:

私は、アレイ持っている:

var surfaces = [  
    {min:0,max:499}, 
    {min:500,max:999}, 
    {min:1000,max:1499}, 
    {min:1500,max:1999}, 
    {min:2000,max:2399}, 
    {min:2400,max:2999}, 
    {min:3000,max:3999}, 
    {min:4000,max:5999}, 
    {min:6000,max:100000} 
] 

を私は$ Qこのlooong $ HTTPチェーン電話を持っています:

$q.when() 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[0]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[1]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[2]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[3]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[4]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[5]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[6]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[7]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 
    .then(function() { 
     $http.post('backend/backend.php?action=get_normes',surfaces[8]).success(function(data){ 
       $scope.normes.push(data); 
     }) 
    }) 

私はむしろループをしたいと思いますが、これを行う方法については今考えてください!!

編集:私はもっと大きな問題を抱えています:データは$ scope.normesで順不同です!常に違う!どのように私はいつも順番に押し込むことができますか?どのようにしてデータを整理できますか?

私はこのようにng-repeatsを取得しましたが、情報は$ httpシンクロニティのためにうまく並べられません。

<div class="row "> 
       <div class="col-lg-10" style="overflow: auto; max-height: 600px;"> 
        <table class="table table-bordered table-hover " border="1" > 
         <thead > 
           <tr> 
           <th> Désignation</th> 
           <th> Total Magasins</th> 
           <th> Moy Habitants Zone</th> 
           <th> Ca Moyen</th> 
           <th> EO Mini</th> 
           <th> EO Maxi</th> 
           <th> Coef Moyen</th> 
           <th> Pourcent Côtier</th> 
           <th> Pourcent Urbain</th> 
           <th> Tx Habituels</th> 
           <th> Tx fréquentation</th> 
           <th> Tx fidélisation</th> 
           <th> Zone Attraction</th> 
           <th> Revenus Zone</th> 
           <th> Nb Perso Foyer</th> 
           <th> Age Moyen</th> 
           </tr> 
         </thead> 

         <tbody > 
          <tr ng-repeat="norme in normes" > 
          <td>Surface 0-499 m²</td> 
          <td>{{::norme.nb_magasins | number:0}}</td> 
          <td>{{::norme.moy_habitants_zone | number:0}}</td> 
          <td>{{::norme.ca_moyen | number:0}}</td> 
          <td>{{::norme.eomin | number:0}}</td> 
          <td>{{::norme.eomax | number:0}}</td> 
          <td>{{::norme.coef_moyen | number:2}}</td> 
          <td></td> 
          <td></td> 
          <td>{{::norme.tx_habituels | number:2}}</td> 
          <td>{{::norme.tx_frequentation | number:2}}</td> 
          <td>{{::norme.tx_fidelisation | number:2}}</td> 
          <td>{{::norme.attraction | number:0}}</td> 
          <td>{{::norme.revenus_zone | number:0}}</td> 
          <td>{{::norme.nb_pers_foyer | number:2}}</td> 
          <td>{{::norme.age_moyen | number:2}}</td> 
          <td ></td> 
          </tr> 


         </tbody> 

        </table> 
       </div> 

$ qをやり直すたびに、別の順序で表示されます。どうすればいい?

EDIT:それはsimplier(Editied HTMLの表は、この記事に)あるので、は、だから私は今、バックエンドからの標準JSONオブジェクトを取得していますが、あなたは優しく提供ソリューションで、それは右ではまだ表示されますdoesntの注文。 $ httpは正しい順序で開始されますが、$ scope.normesは$ httpが開始されたときにリストに表示されません! (オハイオ州私は多分フロントエンドでorderbyと注文することができると思う...私は忘れていたが、私は彼らが配列にプッシュされるようにjsonオブジェクトを注文することが可能だと思ったが、それは表示されません$ HTTP)が呼び出されてい

+0

角度組み込み関数 'forEach'を使用して、複数の非同期httpコールを処理することができます –

答えて

0

)::

$scope.onAllDone = function() { 
    var promises = []; 

    angular.forEach(surfaces , function(surface) { 
     promises.push($http.post('backend/backend.php?action=get_normes',surface)).then(function(data) { 
      $scope.normes.push(data); 
      return data; 
     }); 
    }); 

    return $q.all(promises); 
} 

USE:: 

$scope.onAllDone().then(fnSuccess, fnError); 
+0

ありがとうたくさんありがとうございます。 – user7370387

+0

申し訳ありませんが、このエラーが発生しました。エラー:promises.push(...)。次に関数ではありません。しかし、それは素晴らしいコードと思われる – user7370387

0

約束と$ q.all(docこれを試してみてくださいあなたの友人です。

より詳細には、各呼び出し(呼び出し自体が呼び出しを返さない場合)を約束して配列内にプッシュし、$ q.all(約束).then(allFinished)を呼び出します。

function callUpdate(x, promises) { 
    var d = $q.defer(); 
    $http.post('backend/backend.php?action=get_normes',x).then(function(resp){ 
     $scope.normes.push(resp.data); 
     d.resolve(resp.data); 
    }) 
    promises.push(d.promise); 
} 

... 
var promises = []; 
$scope.normes = []; 
for (var i = 0; i < surfaces.length; i++) { 
    callUpdate(surfaces[i], promises); 
} 

$q.all(promises).then(function() { 
    // called when all promises have been resolved successully 
}); 
+0

はい、それは非常によく動作し、多くのありがとう! unfortunatly私は適切な順序で結果を得ていない、それはdifferente順序で毎回。私はインデックスの数として、配列の配列でng-repeatをやっていると思う...それはそうだ毎回differente orderで$ scope.normesにpuchingして別のインデックスを与えても、私はそれが正しい順序でプッシュすることになっている約束があるので、どのように理解できない。 – user7370387

+0

それはうまく動作します。 :)。ああ、あなたはng-repeatでorderByフィルタを使うことができます。 – digit

+0

$ http.postは非同期です。だから、あなたはどの1が最初に終わるのか分からないでしょう。すべての結果が得られたら、後で注文することができます。 – digit

0

約束あなたは$q.all()を使用して$scope.normes配列を作成するArray.concat()を完了し、使用するすべての約束を待つことができ

var promises = []; 
surfaces.forEach(function(surface){ 
    promises.push($http.post('backend/backend.php?action=get_normes',surface)); 
}) 

の配列を作成します。

$q.all(promises).then(function (results) { 
    $scope.normes = [].concat.apply([],results); 
}); 
+0

非常に明確な、私は今しようとしています。 – user7370387

+0

@nick上記の解決策は機能しましたか? – Rimpy

0

約束をチェーンするとき、それは.thenメソッドハンドラ関数の次の約束リターンに重要である:コードがハンドラ関数に何かを返すことに失敗した場合、$ qを

var arrayPromise = $q.when([]) 
    .then(function (dArray) { 
     var httpPromise = $http.post('backend/backend.php/action=get_normes',surfaces[0]) 
     var dPromise = httpPromise.then(function(response) { 
      $scope.normes.push(response.data); 
      dArray.push(response.data); 
      return dArray; 
     }); 
     return dPromise; 
    }).then(function(dArray) { 
     console.log("one XHR is complete"); 
     console.log(dArray); 
    }); 

サービスは約束をundefinedとして解決し、直ちに次の機能を実行します。後続の約束を返すことによって、$ qサービスは約束を解決するのを待ってからチェーン内の次の約束を実行します。


と言われています。シーケンシャルな約束をつなぐループを作る方法は?

var arrayPromise = $q.when([]); 

for (let n=0; n<surfaces.length; i++) { 
    arrayPromise = arrayPromise 
     .then(function (dArray) { 
     var httpPromise = $http.post(url,surfaces[n]); 
     var dPromise = httpPromise.then(function(response) { 
      $scope.normes.push(response.data); 
      dArray.push(response.data); 
      //return dArray to chain 
      return dArray; 
     }); 
     return dPromise; 
    }); 
}; 

arrayPromise.then(function(dArray) { 
    console.log("all XHRs are complete"); 
    console.log(dArray); 
}).catch(function(errorResponse) { 
    console.log("one of the XHRs failed"); 
    console.log(errorResponse.status); 
    throw errorResponse; 
}); 

空の配列に約束する変数を作成します。ループの中で、.thenメソッドから得られた約束をその変数に戻すだけです。ループの各繰り返しは、前回の反復よりも1項目大きい配列を返します。また、各反復には、アイテムをスコープにプッシュする副作用があります。

関連する問題