【发布时间】:2021-08-18 10:31:06
【问题描述】:
这是我的html文件
<form [formGroup]='itemUpdateForm'>
<div class="row">
<mat-form-field class="col-sm-8">
<mat-label>Restaurant Name:</mat-label>
<input matInput type="text" required [(ngModel)]="this.restuarantService.selectedProduct.name" placeholder="Enter Restaurant Name" formControlName='name'>
</mat-form-field>
</div>
<div class="row" *ngFor='let index of counter(this.restuarantService.selectedProduct.Items.length) ;let i = index' formGroupName='Items'>
<mat-form-field class="col-sm-5">
<mat-label>Food Name:</mat-label>
<input matInput type="text" required [(ngModel)]='this.restuarantService.selectedProduct.Items[i].foodName' placeholder="Enter Food Name" formControlName='foodName'>
</mat-form-field>
<mat-form-field class="col-sm-5" >
<mat-label>Price:</mat-label>
<input matInput type="text" required [(ngModel)]="this.restuarantService.selectedProduct.Items[i].price" placeholder="Enter Price" formControlName='price'>
</mat-form-field>
</div>
</form>
我的输出看起来像这样 Output
最后一个值会覆盖所有字段中的先前值。所以我得到了所有字段中的最后一个值..我想按顺序显示所有值。我需要做什么改变
【问题讨论】:
-
你为什么同时使用 formcontrolname 和 ngmodel?请分享更多代码以更好地理解
-
如何以反应形式绑定这些值?像双向绑定
-
分享一个工作示例(stackblitz)
-
@VenkateshPrasath,如果您使用反应式表单,请使用 setValue、angular.io/api/forms/FormGroup#setvalue 或 pathValue angular.io/api/forms/FormGroup#patchvalue 或在创建 formGroup angular.io/api/forms/FormControl#usage-notes 时提供值,不使用 [(ngModel )]
标签: angular angular-ngmodel ngmodel