0

I am a beginner in angular world.
The problem I'm facing is that i have a custom array ($scope.picking.operations) and i can add many objects to this array (with a boolean send set to false), then there is a button that sends this data to the server through angular's $resource service, and in the success callback I want to change this boolean sent to true; But console.info(op) returns the first element of the array, and not the current element.
Any ideas will be appreciated. Thank You.

  $scope.sendToServer = function() {
    ops = utils.reduceArrayByMoveId($scope.picking.operations);  // Custom service returns [{..}, {..}, ...]
    $scope.picking.operations = ops;
    for (var i = 0, len = $scope.picking.operations.length; i < len; i++) {
      op = $scope.picking.operations[i];
      if (op.sent) {
        continue;
      }
      op_to_create = {
        stock_move_id: op.stock_move_id,
        quantity: op.quantity,
      }
      console.log("Operation to create: ", op_to_create);
      var tra = new Transaction(op_to_create);
      tra.$save(
      function(value, responseHeaders) {
        console.info(value);
        op.sent = true;
        console.info(op);
      },
      function(httpResponse) {
        notice = 'Conection Error: [' + httpResponse.status + ']: ' + (httpResponse.statusText||'Connection refused');
        $state.go('404', {obj: {info: notice}});
      });
    }
  };
ssaid
  • 65
  • 8

1 Answers1

0

Here explains the javascript closure problem that I faced in this question: Javascript closure inside loops, actually the problem was solved implementing angular.forEach.

Community
  • 1
  • 1
ssaid
  • 65
  • 8