简体   繁体   English

AngularJs $ q。全部使用

[英]AngularJs $q.all use

i am using multiple asynchronous calls, I do get the result but when i want to use the arrays where i saved this result a lot of problems appear, when I looked i found that the solution is using $q.all which i never used before and when i read the documentation i still didn't understand where i should add it, here is my code : 我正在使用多个异步调用,但确实得到了结果,但是当我想使用保存该结果的数组时,出现了很多问题,当我看时发现该解决方案正在使用$ q.all,而我以前从未使用过当我阅读文档时,我仍然不知道应该在哪里添加它,这是我的代码:

FT.getFT().then(function (result) {       
               if(result.data.success)
               {   
                for(var i=0; i<result.data.ftListe.length;i++) 
               {
                // récupérer le collaborateur de la feuille de temps par son id  
                  Compte.getComptesbyId(result.data.ftListe[i].collaborateurid).then(function(result)
                   {
                      lecollaborateur.push(result.data.col);
                   }); 

                //Get the name of the task related to the timesheet
                 Tache.getTachebyId(result.data.ftListe[i].tacheid).then(function(result)
                   {
                     Projet.getProjetbyId(result.data.tachelistes.projet_id).then(function(result)
                                {
                                  projets.push(result.data.projetsListe);
                                });    
                    task.push(result.data.tachelistes);    
                 }); 
                     // get le projet lié à cette tache par id  


              }
             $scope.ftListe = result.data.ftListe;
             $scope.task = task;
             $scope.lecollaborateur = lecollaborateur;
             $scope.projets = projets;
            console.log(result.data.message);
            }else{
                console.log(result.data.message);
            }

        });

I can show how $q.all() works and write the example for one of your promises. 我可以展示$q.all()工作原理,并为您的一个诺言编写示例。 Then is up to you to refactor all your code in the same way. 然后由您以相同的方式重构所有代码。 I can also keep variable names in French :) 我也可以用法语保留变量名:)

$q.all() allows you to resolve an array of promises which you give as parameter to all() . $q.all()允许您解析作为参数提供给all()的promise数组。 You will have as result of the promise an array where the elements are the corresponding results to your promises. 作为promise的结果,您将获得一个数组,其中元素是您的promise的相应结果。

So, in your case, inside your main result, create an array of promises like this: 因此,就您而言,在您的主要结果内,创建一个如下的promise数组:

FT.getFT().then(function (result) {       
  if(result.data.success) {   
    var comptesPromises = [];

Inside your for, instead of resolving promises, just add to this array all your corresponding promises, like this: 在您的for内部,而不是解决承诺,只需将所有相应的承诺添加到此数组中,如下所示:

comptesPromises.push(Compte.getComptesbyId(result.data.ftListe[i].collaborateurid));

Then outside your for, resolve all the promises and assign the returning values to your model: 然后在for之外,解决所有promise,并将返回值分配给模型:

$q.all(comptesPromises).then(function(comptes) {
  comptes.forEach(function(el) {
    lecollaborateur.push(el.data.col);
  });
});

You need to inject $q in order to use it. 您需要注入$q才能使用它。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM