Harsh Harsh - 6 months ago 20x
Javascript Question

How to do name="skills[]" in angularjs in input type text?

In php usually we are doing name="skills[]" to get data from form as array but how to do this in angularjs?
PHP example:

<input type="text" name="skills[]" />

I want to do the same in AngularJS but getting syntax error.
I am trying like this:

My code:

<input type="text" name="skillname" required data-ng-model="c.skills[].skillname" class="small">
<input type="text" name="skillname" required data-ng-model="c.skills[].skiilname" class="small">

Need help. Thanks.


In angular, data rules your UI, not the other way around. You should have skills array in your controller

app.controller('MainCtrl', function($scope) {
  $scope.skills = [

which you'll feed to ng-repeat or something.

<div ng-repeat='skill in skills'>
  <input ng-model='skill.name' />

This way, adding new textfields on the page is as easy is pushing a new element to the array, $scope.skills.push({}). No need to create DOM elements or anything. This is the angular way.

Demo: http://plnkr.co/edit/Uqldnst3gnF07SJGV6Bn?p=preview