How to use a ng-model with array

0

This is my code link

I want to save one or several objects in an array, I have two selects with the same options, the user decides whether to fill the default option or fill several,

It should be stored as follows:

likes[
  {sport: 'futball', points: 1}, {sport: 'tennis', points: 1}
]

but how could I save the data in an array in the ng-model of each select?

because I want to use something like ng-model="array[$index].sport" or .point

I do not know if it's possible

    
asked by Pxion 12.04.2016 в 00:43
source

2 answers

0

The way is to create an array in your controller.

$scope.coleccion = [] ;

Then in the HTML you put something like that ng-model="coleccion[0].deporte"

Example:

<selectize placeholder='Pick some things...' options='myOptions' 
           config="{maxItems: 1}" ng-model="coleccion[0].deporte" 
           required='true'></selectize>

<input type="number" min="1" ng-model="coleccion[0].puntuacion" required>

This is your example with the changes applied ...

link

Remember to press "save" to see how the result is.

    
answered by 12.04.2016 / 00:57
source
0

if the ng-model is within a ng-repeat :

  

Driver > $scope.likes = [];

     

Vista > ng-model="likes[$index]['sport']"

that will work well for you.

try it first, apparently not yet testing what you yourself say.

    
answered by 22.07.2016 в 21:42