【发布时间】:2020-07-05 15:41:07
【问题描述】:
我有一个返回对象数组的 API 服务。每个对象都有两个字段名称和用法。我正在动态创建一个表单,其中表单字段的数量等于数组中的对象数量。表单字段的名称是name,它应该预先填充usage 值。我能够创建表单并命名表单字段,但 usage 值未在每个表单字段中显示正确的值。
我的 TS 文件代码:
ngOnInit() {
this.apiCall.getArray()
.subscribe(
(res: any) => {
this.receivedData = res;
for (let i = 0; i < this.receivedData.length; i ++){
this.currentName = this.receivedData[i].name;
this.currentUsage = this.receivedData[i].usage;
this.name.push(this.currentName);
this.usage.push(this.currentUsage);
}
name 和usage 是存储值的数组。在我的 HTML 中:
<form>
<div class="form-row">
<div class="form-group *ngFor="let plans of name">
<label>{{plans}}</label>
<input type="number" min="0" class="form-control" id="usage" name="usage" value="usage" [(ngModel)]="usage">
</div>
</div>
</form>
现在我的表单字段显示正确的字段名称,但字段为空。它们应预先填充各自的使用值,并且用户应该能够根据需要更改值。
【问题讨论】:
标签: angular typescript forms angular8