Search code examples
angularjsangularjs-ng-repeat

Use ng-repeat and $index to set the model property


Can I use ng-repeat and $index to set a model property dynamically?

I'm trying to do something like this:

<div ng-repeat="x in [].constructor(10) track by $index">
  <input ng-model="model.prop{{$index}}">    
</div>

And I want my model to end up like this (i.e. so these properties don't exist in the controller code, I want them to be dynamically added):

{
  "prop0": "val",
  "prop1": "val",
  "prop2": "val",
  "prop3": "val",
  "prop4": "val",
...
  "prop10": "val"

  "otherProperty": "xxx"
}

I don't want the properties to be an array and I need to do this without changes to the controller.

Thanks


Solution

  • Sorted, I did this:

    <div ng-repeat="x in [].constructor(10) track by $index">
      <input ng-model="model.['prop' + ($index)]">    
    </div>