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,642
  • 3
  • 20
  • 25
Vindya Veer
  • 139
  • 1
  • 2
  • 15

3 Answers3

1

try:

ng-model="inputfields[field.Name]"
pixelbits
  • 50,631
  • 15
  • 93
  • 132
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,724
  • 1
  • 13
  • 15
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,416
  • 9
  • 20