問題描述
在 AngularJS 中保存在 ng‑repeat 中生成的字段 (Saving a field generated in ng‑repeat in AngularJS)
所以,我需要在數組中保存 ng‑repeat 中生成的每個字段的數據。我的代碼看起來像這樣:
<tr ng‑repeat = "feature in filteredFeatures">
<td>{{feature.name}}</td>
<td><input type="number" ng‑model=""></td>
</tr>
所以,無論用戶在輸入字段上寫什麼,我都希望保存它,但我當然不知道如何保存。
參考解法
方法 1:
Just add a model to ng‑model in the input
<tr ng‑repeat = "feature in filteredFeatures">
<td>{{feature.name}}</td>
<td><input type="number" ng‑model="feature.name"></td>
</tr>
方法 2:
All right it wasn't that hard after all. As suggested I put an array via ng‑model, but the trick was in using $index (I had no idea I could use it). So the code ended looking like this:
<table class="table">
<tr>
<th>Feature</th>
<th>Result</th>
</tr>
<tr ng‑repeat = "feature in filteredFeatures">
<td>{{feature.name}}</td>
<td><input type="number" ng‑model="measures[$index]"></td>
</tr>
</table>
So, hope I can help someone in the future, and thank to everyone who provided a little bit of their time.
(by Gabo Eremita、stalin、Gabo Eremita)