0

I have a object I have iterated using ng-repeat and while iterating I want to add each field to another object through ng-model.

//html 

<div ng-repeat="field in forms.fields track by $index">
    <span ng-if="field.type=='textbox'">
    <span style="width:115px;text-transform: capitalize" class="col-lg-5">{{field.Name}}</span>
    <input type="text" class="form-control" style="width: 172px;padding-left: 20px;display: inline-block;" ng-class="{mystyle:field.Name.$invalid}" id="inp_{{$index}}" ng-model="inputfields.{{field.Name}}">{{field.Name}}<br>
</span></div>

In the above code i want to assign field.Name as a property to input field which is declared as a $scope.inputfield={} in js file. So i am getting an error as

 Syntax Error: Token '{' invalid key at column 2 of the expression [{{inputfield.field.Name}}] starting at [{inputfield.field.Name}}].

So can anyone solve this please.

PravinS
  • 2,640
  • 3
  • 21
  • 25
Vindya Veer
  • 139
  • 1
  • 3
  • 15

3 Answers3

1

try:

ng-model="inputfields[field.Name]"
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
1

use with array notation to get the property of the object

ng-model="inputfields[field.Name]"

instead of

ng-model="inputfields.{{field.Name}}"
Shushanth Pallegar
  • 2,832
  • 1
  • 14
  • 16
1
inputfields.{{field.Name}} is invalid expression assignment.

You can assign fidlename to inputfields using ng-model="inputfields[field.Name]"

for more detail refer this link : https://docs.angularjs.org/error/ngModel/nonassign

Juned Lanja
  • 1,466
  • 10
  • 21