0

I have the following array:

$scope.variations = [
    {'nom':'',
     'preu':0,
     'grams':''
    },
    {'nom':'',
     'preu':0,
     'grams':''
    },
    {'nom':'',
     'preu':0,
     'grams':''
    }
];

And I display it in the frontend like so:

<div ng-repeat="vari in variations">
  <input type="text" class="inputmodal" ng-model="varinom">
  <input type="text" class="inputmodal" ng-model="varipreu">
  <input type="text" class="inputmodal" ng-model="varigrams">
</div> 

Once I fill the 3 inputs in all 3 items of the array, I'm trying to push each one of them into another array, like so:

<a ng-click="test()">TEST</a>

And the JS:

 $scope.test = function(){
   $scope.singleorder = [];
   for(var i = 0; i < $scope.variations.length; i++)
     $scope.singleorder.push({
       'nom': $scope.variations[i].varinom,
       'grams': $scope.variations[i].varigrams,
       'preu': $scope.variations[i].varipreu,
     });

  console.log($scope.singleorder);
};     

On my results in the console, I have 3 arrays in the console, but the values of each object are undefined.

What am I missing?

2
  • 2
    It should be ng-model="vari.nom" not ng-model="varinom" Commented Feb 25, 2017 at 20:36
  • Put it as an answer and i vote u up! Commented Feb 25, 2017 at 20:38

1 Answer 1

1

You have set ngModel correctly. use reference variable vari like

<div ng-repeat="vari in variations">
  <input type="text" class="inputmodal" ng-model="vari.nom">
  <input type="text" class="inputmodal" ng-model="vari.preu">
  <input type="text" class="inputmodal" ng-model="vari.grams">
</div> 

and use

 $scope.singleorder.push({
   'nom': $scope.variations[i].nom,
   'grams': $scope.variations[i].grams,
   'preu': $scope.variations[i].preu,
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.