【发布时间】:2019-11-15 16:58:29
【问题描述】:
我有一个功能,我需要在单击按钮时发送动态生成的输入字段。
为了更好地理解,我在stackblitz 上重现了这个问题。
在该应用程序中,当我输入 resourceQuantity 时,会动态生成 resourceId 字段。 我的问题是单独识别这些字段并通过单击按钮将它们发送到服务器端。
This 我在 stackblitz 上找到的解决方案类似,但在我的问题中,我不是删除或添加按钮点击,而是(更改)事件。
这是 HTML 代码:
<mat-form-field>
<input matInput type="number" formControlName="resourceQuantity" [(ngModel)]="resourceQuantity" placeholder="Enter Resource Quantity" (change)="somethingChanged()"/>
</mat-form-field><br/><br/>
<div>
<ul>
<li *ngFor="let item of counter(resourceQuantity)">
<input matInput type="number" placeholder="Enter Resource Number" formControlName="resourceId"/>
</li>
</ul>
</div>
这是 TS 代码:
ngOnInit() {
this.form = new FormGroup({
'employeeId': new FormControl(null, {validators: [Validators.required]}),
'employeeName': new FormControl(null, {validators: [Validators.required]}),
'resourceQuantity': new FormControl(null, {validators: [Validators.required]}),
'resourceId': new FormControl(null, {validators: [Validators.required]})
});
}
somethingChanged() {
console.log(this.resourceQuantity);
}
counter(i: number) {
return new Array(i);
}
请告诉我解决问题的最佳方法。谢谢。
【问题讨论】:
-
你的意思是说你想要这样的东西: {empId:1, empName:"abc", resourceQuantity:2, resourceId:11, resourceId:22} ? @Rachit
标签: angular typescript angular-reactive-forms angular-forms angular-in-memory-web-api