Form input arrays in angular

Daniel White picture Daniel White · Jan 26, 2015 · Viewed 11.8k times · Source

I am learning Angular.js and i've come to a problem that should probably be simple, but I can't seem to find an answer on.

I want to create form inputs with the value of "connectedTeams", like this in html:

<input type="text" name="connectedTeam[]"> 
<input type="text" name="connectedTeam[]"> 
<input type="text" name="connectedTeam[]"> 

I have tried the following in angular...

<input type="text" name="connectedTeams[]" class="form-control" ng-model="user.connectedTeams">

...but it is binding the same value to all 3 inputs. I know in my mind that this makes sense, but I can't seem to figure out how to tell it that ng-model is user.connectedTeams.[] (user > connectedTeams > add to an array.

I hope this makes sense enough for someone to provide a quick answer.

Answer

maxisam picture maxisam · Jan 26, 2015
 ng-model="user.connectedTeams[0]"
 ng-model="user.connectedTeams[1]" and so on

You can put it in a ngRepeat, so you don't need to repeat your code like above.

FYI, name is only used for validation in Angularjs.