0

I'm using angular 1.6

I'm looking for a way to pass a key into an ng-model. The same way you would by doing something like this in HTML

id="{{key}}-general"

For example imagine you had this:

<div ng-repeat="detail in details">
  <input type="text" ng-model="telephone[0]" />
  <input type="text" ng-model="telephone[1]" />
</div>

I want to pass the $index of the repeat into those ng-model so it creates a new array. I want to be able to do the following

<input type="text" ng-model="telephone[{{$index}}] />

Is this even possible?

Mike Young
  • 321
  • 2
  • 4
  • 15
  • Good question but seemingly a duplicate -- I think you might find what you need in the top answer in the linked question – Dexygen Apr 30 '18 at 16:28

1 Answers1

1

telephone[$index] will work if you have scope model for telephone.

http://jsfiddle.net/U3pVM/47614/

Html

<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="TodoCtrl">
    <div ng-repeat="detail in details">
      <input type="text" ng-model="telephone[$index]" />
    </div>
  </div>
</div>

Controller

function TodoCtrl($scope) {
  $scope.details = ["test","test","test","test","test","test"];
  $scope.telephone = ["phone1","phoneX",2,3,4,5,6];
}
hurricane
  • 6,521
  • 2
  • 34
  • 44