[英]Get Data from Dynamically Created textboxes in Angular using ng-for
我已經創建了一組像這樣動態創建的文本框
<input id="Pun{{item}}" type="text"
class="form-control textbox" placeholder=Punishment >
</div>
其中,penaltynum 是一個數字數組。 我想知道如何從每個創建的文本框中獲取數據,不勝感激。
我在stackblitz上創建了一個解決方案。
使用由*ngFor
值迭代的值更新每個輸入,如下所示。
<div *ngFor="let item of items">
<input id="Pun{{item}}" type="text" [(ngModel)]="pun[item]"
name="pun[item]" class="form-control textbox" placeholder="punishment
{{item}}" > {{pun}}
</div>
在哪里
items = [1, 2, 3, 4];
pun = [];
您可以使用 document.getElementById()。
您的 HTML 文件:
<input id="Pun{{item}}" type="text" class="form-control textbox" placeholder=Punishment>
你的 ts 文件:
var item="YOUR_ITEM_HERE";
var textValue = (document.getElementById("Pun"+item) as any).value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.