1

I'm trying to concatenate an ng-repeat item to ng-model object. I was wondering if this possible.

so for example:

//array
$scope.array = [
 {
   param: 'color',
   ....
 },
 {
   param: 'weight',
   .....
 }
]

html

<div ng-repeat="item in array">
  {{ item.param }}

   <input type="text" ng-model="form.name.{{ item.param }}" >
</div>

so lets say {{ item.param }} is color, the ng-model will be form.name.color.

form object will be something like this:

{

  name: {
   color: 'value of input',
   weight: 'value of input'
  }

}

How can I concatenate the item.param to the object form.name? I've been trying so many ways but no results. I trying to use $index, but don't know where to begin.

Your help will be appreciated!

medev21
  • 2,469
  • 8
  • 31
  • 43
  • possible duplicate http://stackoverflow.com/questions/14410993/binding-ng-model-inside-ng-repeat-loop-in-angularjs –  Dec 02 '16 at 01:10
  • @fubbe This could be a duplicate if I didn't have `form.name` in the `ng-model`, in that case it'll just be `ng-model = "item.param"`. I need `form` object, because I'm sending this as new object, and would like to have the `item.param` as a key in the `form.name` object – medev21 Dec 02 '16 at 01:53

1 Answers1

1

Check out this fiddle.

Your ng-model should look like this:

ng-model="form.name[item.param]"
Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47