angularjs - Create a dynamic ng-model for an input -


how can make ng-model of input dynamic?

static ng-model:

<input type="text" ng-model="mymodel.firstname" /> 

dynamic ng-model:

$scope.myinputs = [{ key: "firstname"}, { key: "lastname" }];  <div ng-repeat="input in myinputs">   <input type="text" ng-model="mymodel[input.key]" /> </div> 

the mymodel[input.key] not seem calculating correctly.

in myinputs[input.key], input.key not index. cannot access expected value.

you can either

<div ng-repeat="input in myinputs">   <input type="text" ng-model="input.key" /> </div> 

or

<div ng-repeat="input in myinputs track $index">   <input type="text" ng-model="myinputs[$index].key" /> </div> 

Comments