簡體   English   中英

ng-repeat輸入在每個循環中保持相同的值

[英]ng-repeat input keeping same value for each loop

我在循環內有一個帶有評論輸入的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> 

由於您處於循環中,因此每個循環的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>

在控制器中,這將是一個更大的對象,因此對於發現裝載中的兩個項目的循環,您將在comss中使用它:

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

在模板中,您無法通過comss.0.comment訪問它,這就是為什么在分配模型時會在循環中使用comss [$ index] .comment的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM