Angularjs $http在循环中,等待结果

Angularjs $http在循环中,等待结果,angularjs,synchronization,promise,http-post,Angularjs,Synchronization,Promise,Http Post,我有这个: var j = 0; // myList.length = 3 while(j < self.myList.length - 1){ $http.post('myURL', self.myList[j].code).then( function(response){ self.myList[j].plop = response.data; }, function(){

我有这个:

var j = 0;

// myList.length = 3
while(j < self.myList.length - 1){

    $http.post('myURL', self.myList[j].code).then(
            function(response){

                self.myList[j].plop = response.data;

            }, function(){

                // error

            }
        ).then(
            function(){
                // with this j++, my web page is freezing
                // j++;
            }
        );

    // with this j++, only the 3rd element of myList have a "plop" element
    //j++;
}
var j=0;
//myList.length=3
而(j
我的问题在注释中:)表示“j++”。 如果我在执行第3步时移除了循环并对其进行了硬编码,那么它就正常工作了。但我不知道如何用循环解决这个问题。你有什么想法吗?谢谢

问题是

  • $http.post()。然后(function(){},function(){})
    是异步函数。所以
    .then()
    方法将在循环完成时执行。这就是它总是取j的最后一个值2的原因
  • then()
    只会与
    $http.post()
    一起出现一次,所以删除第二次
    。then()
  • 解决方案发布如下:

    var j = 0;
    var i = 0;
    
    // myList.length = 3
    while(j < self.myList.length - 1){
        $http.post('myURL', self.myList[j].code).then(function(response){
                    self.myList[i].plop = response.data;
                    i++;
    
                }, function(){
                    // error
                }
            );
    }
    
    var j=0;
    var i=0;
    //myList.length=3
    而(j
    问题是

  • $http.post()。然后(function(){},function(){})
    是异步函数。所以
    .then()
    方法将在循环完成时执行。这就是它总是取j的最后一个值2的原因
  • then()
    只会与
    $http.post()
    一起出现一次,所以删除第二次
    。then()
  • 解决方案发布如下:

    var j = 0;
    var i = 0;
    
    // myList.length = 3
    while(j < self.myList.length - 1){
        $http.post('myURL', self.myList[j].code).then(function(response){
                    self.myList[i].plop = response.data;
                    i++;
    
                }, function(){
                    // error
                }
            );
    }
    
    var j=0;
    var i=0;
    //myList.length=3
    而(j
    根据OP的评论,同步解决方案:

    var promises = [];
    for(var i=0;i<self.myList.length;i++)
     promises.push($http.post(...));
    $q.all(promises).then(function(results){
     //All results available here
     var data = results.map(result => result.data);
     data.forEach((e, idx) => self.myList[idx] = e);
    })
    .catch(function(e){
     //Handle error
    });
    
    var承诺=[];
    对于(var i=0;i result.data);
    data.forEach((e,idx)=>self.myList[idx]=e);
    })
    .catch(函数(e){
    //处理错误
    });
    
    根据OP的评论,同步解决方案:

    var promises = [];
    for(var i=0;i<self.myList.length;i++)
     promises.push($http.post(...));
    $q.all(promises).then(function(results){
     //All results available here
     var data = results.map(result => result.data);
     data.forEach((e, idx) => self.myList[idx] = e);
    })
    .catch(function(e){
     //Handle error
    });
    
    var承诺=[];
    对于(var i=0;i result.data);
    data.forEach((e,idx)=>self.myList[idx]=e);
    })
    .catch(函数(e){
    //处理错误
    });
    
    谢谢,它工作得更好,但并不完全有效。事实上,$post是并行执行的。因此,var“i”不是同步的。谢谢,它工作得更好,但不是完全同步的。事实上,$post是并行执行的。因此,变量“i”是不同步的。