SARUAV
SARUAV

Reputation: 188

ng-repeat input keeping same value for each loop

I have an ng-repeat with a comment input inside the loop. With a ng-model="comss.comment", however when I start to type on the first input, I can see the typing on the 2nd and all of the other inputs. How can I stop this? I've tried adding a name with a unique ID however that did not work.

Here is my code:

<li class="item" style="margin-top:20px;" ng-repeat="schedule in discoverloaded | filter:scheduleSearch | limitTo:numberOfItemsToDisplay">

<input type="text" ng-model="comss.comment" required='required' placeholder="Write a comment..">

</li>

Upvotes: 0

Views: 128

Answers (1)

rrd
rrd

Reputation: 5957

Since you're in a loop, accessing comss.comment for each loop is going to be the same model, you need to modify your template and the model slightly:

<li class="item" style="margin-top:20px;"
  ng-repeat="schedule in discoverloaded | filter:scheduleSearch | limitTo:numberOfItemsToDisplay track by $index">

  <input type="text" ng-model="comss[$index].comment"
    required='required' placeholder="Write a comment..">

</li>

In the controller it would be a larger object, so for a loop of two items in discoverloaded, you would have this in comss:

comss = {
  0: {
    comment: ''
  },
  1: {
    comment: ''
  }
};

In the template you can't access it via comss.0.comment, which is why you use comss[$index].comment as you're inside the loop when you assign the model.

Upvotes: 1

Related Questions