ng-repeat输入为每个循环保持相同的值

时间:2017-08-15 07:36:46

标签: javascript angularjs

我在循环中有一个带有注释输入的ng-repeat。使用ng-model =" comss.comment"但是当我开始输入第一个输入时,我可以看到第二个输入和所有其他输入的输入。我怎么能阻止这个?我尝试添加一个带有唯一ID的名称,但这不起作用。

这是我的代码:



<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

由于您处于循环中,因此访问每个循环的comss.comment将是相同的模型,您需要稍微修改模板和模型:

<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>

在控制器中它将是一个更大的对象,因此对于在discoverloaded中的两个项目的循环,你可以在comss中使用它:

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

在模板中,您无法通过comss.0.comment访问它,这就是您在分配模型时使用comss [$ index] .comment进行循环的原因。