I have a JSON object in my controller and I would like to dynamically generate input fields using ng-repeat.
$scope.keyValuePairs = [
{id:""},
{type:""},
{brand:""},
{category:""},
{subCategory:""},
{division:""}
];
And in the template:
<div class="mdl-cell mdl-cell--6-col-desktop mdl-cell--8-col-tablet"
ng-repeat="keyValue in keyValuePairs track by $index">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label full-width"
ng-repeat="(key,value) in keyValue">
<input type="text" id="{{key}}" class="mdl-textfield__input" ng-model="eanForm[key]"
name="{{key}}" ng-required="true"/>
<label class="mdl-textfield__label" for="{{key}}">Enter {{key}}</label>
<span class="mdl-textfield__error" ng-show="eanForm.id.$dirty &&
eanForm.id.$invalid" for="{{key}}">{{key}} required</span>
</div>
</div>
I am looking for a way to create dynamic ng-models so that users can add or delete form fields. How can I bind form fields to dynamic models?