【发布时间】:2021-04-07 20:53:02
【问题描述】:
升级到最新版本的 angular(11) 和 typescript 4 后,表单的嵌套值的设置停止工作,有人可以帮助我吗?
之前有效但现在无效的线路是: control.at(+z).get('unitTotalPrice').setValue(totalUnitPriceFormatted, {onlySelf: true, emitEvent: false});
在一些编辑过的代码下面应该显示需要显示的所有内容
ngOnInit()
{
.......
this.myFormValueChanges$ = this.myorderForm.controls['itemRows'].valueChanges;
this.myFormValueChanges$.subscribe(itemRows => this.updateTotalUnitPrice(itemRows));
this.myorderForm = this.formBuilder.group({
userName: [this.CurrentUserName],
.....
itemRows: this.formBuilder.array([this.initItemRows()]),
})
initItemRows() {
const numberPatern = '^[0-9.,]+$';
return this.formBuilder.group({
sub_orderNumber : [''],
.......
unitTotalPrice: [''],
......
},);
.....
}
}
updateTotalUnitPrice(itemRows: any) {
const control = <FormArray>this.myorderForm.controls['itemRows'];
......
for (let z in itemRows) {
let totalUnitPriceFormatted = this.currencyPipe.transform(totalUnitPrice, 'GBP', '','1.2-2');
control.at(+z).get('unitTotalPrice').setValue(totalUnitPriceFormatted, {onlySelf: true, emitEvent: false});
console.log("what is totalUnitPriceFormatted here", totalUnitPriceFormatted)
.......
this.checkvalues();
}
}
checkvalues(){
let arr = <FormArray>this.myorderForm.controls.itemRows;
console.log("what is arr here THIS IS THE WHOLE LINE", arr.value)
}
the console.log in chrome says...
what is totalUnitPriceFormatted here xxx where xxx is the correct value
and
what is arr here THIS IS THE WHOLE LINE
modified_date: "2021-04-06 16:22:08"
.......
unitTotalPrice: "0.00"
........
where uniTotalPrice contains the wrong information (0.00)
Please notice the checkvalues function is there just to check all the values of the array.
【问题讨论】:
标签: angular typescript forms