在angularJS + express中使用$ q.all重复响应

时间:2022-10-17 00:19:41

When using $q.all() to get multiple responses, I get the same values in the response object. I get as many objects back as promises declared, but all the 'name' fields have the same value (the last one, 3).

当使用$ q.all()获取多个响应时,我在响应对象中获得相同的值。我获得了与声明的promises一样多的对象,但所有'name'字段都具有相同的值(最后一个,3)。

.controller('myCtrl', function ($scope, $state, $q, myService) {

   $scope.myList = [];

   $scope.create = function() {
      var newObject;
      var promises = [];
      for(var i = 0; i < 4; i++){
        newObject = { name: i };
        promises[i] = myService.create(newObject);
      }
      $q.all(promises).then(
       function (response) {
         $scope.myList = response;
       }
      );
   };
}

And here's my service:

这是我的服务:

    .service('myService', function ($http, $q, baseURL) {

       this.create = function(object) {
         var deferred = $q.defer();
         //console.log shows that object still has the proper 'name' value
         $http.post(url, object).then(
            function (response) {
              // console.log shows that all response objects have the same 'name' value.
              deferred.resolve(response);
            }
          );
         return deferred.promise;
       };
}

Any input is appreciated since it's my first approach to promises in Angular.

任何输入都是值得赞赏的,因为这是我第一次接受Angular中的承诺。

2 个解决方案

#1


2  

Solved by moving the newObject declaration inside the loop, as we redeclare the variable, the promise keeps a copy of the previous value to itself.

通过在循环中移动newObject声明来解决,当我们重新声明变量时,promise会将前一个值的副本保留给自身。

#2


0  

I'm not really sure what it is you are trying to do, but the way you have your code written, it will always end up with the name of the last one because you are over-writing your promise each time. You would need to do something similar to the following:

我不确定你要做的是什么,但是你编写代码的方式总是会以最后一个的名字结束,因为你每次都在写你的承诺。您需要执行类似以下操作:

      var newObject;
  var promises = [];
  for(var i = 0; i < 4; i++){
    newObject = { name: i };
    var promise = myService.create(newObject);
   promises.push(promise);
  }
  $q.all(promises).then(
   function (response) {
     $scope.myList = response;
   }
  );

};

#1


2  

Solved by moving the newObject declaration inside the loop, as we redeclare the variable, the promise keeps a copy of the previous value to itself.

通过在循环中移动newObject声明来解决,当我们重新声明变量时,promise会将前一个值的副本保留给自身。

#2


0  

I'm not really sure what it is you are trying to do, but the way you have your code written, it will always end up with the name of the last one because you are over-writing your promise each time. You would need to do something similar to the following:

我不确定你要做的是什么,但是你编写代码的方式总是会以最后一个的名字结束,因为你每次都在写你的承诺。您需要执行类似以下操作:

      var newObject;
  var promises = [];
  for(var i = 0; i < 4; i++){
    newObject = { name: i };
    var promise = myService.create(newObject);
   promises.push(promise);
  }
  $q.all(promises).then(
   function (response) {
     $scope.myList = response;
   }
  );

};