[英]Angular PatchValue is not working with FormArray
我正在嘗試修補一個值,所以我的輸入就像ng模型一樣
這是代碼
this.purchaseOrderForm.patchValue({
itemForm: {
inputSubTotal: this.tempSellPrice.valueOf().toString()
}
});
但這不起作用
this.purchaseOrderForm.at(i).patchValue({
itemForm: {
inputSubTotal: this.tempSellPrice.valueOf().toString()
}
});
也無法正常工作,我=索引計數。
有沒有一種方法可以保存我的表單呢?
不幸的是,您不能直接在表單數組上修補或設置值,而是需要在其中修補或設置表單組/控件的值,如下所示:
onChangeState(i){
const fg = this.itemsArray.at(i);
const fgValue = fg.value;
fg.patchValue({
total: fgValue.fvalue + fgValue.svalue
});
}
這是一個閃電戰演示: https : //stackblitz.com/edit/angular-wyly3s? file = src/app/ app.component.ts
我不能在不知道您正在使用的表單的真實結構的情況下說出您的問題代碼中到底有什么問題
我在這里查看了您的StackBlitz示例https://stackblitz.com/edit/angular-c6xqxm ,我想我發現了問題。 您必須將[formGroupName] =“ 1”嵌套在* ngFor的子元素中
代替
<div *ngFor="let group of itemsArray.controls; let i = index;" [formGroupName]="i">
//your code...
</div>
嘗試
<div *ngFor="let group of itemsArray.controls; let i = index;" >
<div [formGroupName]="i"> <----------
//your code...
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.