Amit Kumar
Amit Kumar

Reputation: 5962

Generate Dynamic ng-model for form Control in angularJS

I have an Array in my controller. On the basis of that Array I'm generating Input fields On my page.

My AngularJs code

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.names = ['morpheus', 'neo', 'trinity'];
});

And on page I'm generating my input fields

<form name="myForm1" ng-controller="MainCtrl">
     <div ng-repeat="gg in names">
       <input type="text" ng-model="control[index]"/>
     </div>
     <input type="submit" value="submit"/>
</form>

Now it generating ng-model for each textbox are control[index]

but I want to generate ng-model for each textbox like

control[0]
control[1]
control[2]

Plunker

Upvotes: 2

Views: 632

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

you have to use

<input type="text" ng-model="control[$index]"/>

and there is no scope variable called control so you need to define the scope variable also, as below

$scope.control = {};

here is the updated Plunker

Upvotes: 3

Related Questions