Ehsan Ehsan - 7 months ago 23
Javascript Question

Dynamic ngModel object attribute inside ngRepeat

This is my code, I want to use dynamic object as ng-model:

<div class="form-group" ng-repeat="(key,value) in importdata.fields">
<label class="col-sm-3 control-label" >
{{value}}
</label>
<div class="col-sm-9">
<select ng-model="form.{{key}}" ng-options='column as column for column in importdata.columns' class="form-control"></select>
</div>
</div>
{{form | json }}


I want to show result like this:

{
name:'john',
family:'Doe'
}


or

{
name:'john',
family:''
}

Answer

You need to use bracket notation to access dynamic property by variable name:

ng-model="form[key]"

By the way, there is nothing Angular specific here, the same in normal JS.

Comments