[英]Reactive Forms patchValue() in For loop
Following sample code:以下示例代码:
model={name: "D100"
value: "Dying"};
for (let key in model) {
if(this.divisionForm.controls.hasOwnProperty(key)) {
let value = divisionModel[key];
this.form.patchValue({
key: value
});
}
}
I have structed in this for long time kindly help me.我已经在这方面建立了很长时间,请帮助我。 Thanks in advance.
提前致谢。
Simply spread your form value and override the properties you want to change.只需传播您的表单值并覆盖您想要更改的属性。
As a sidenote, patchValue
is supposed to keep the values of the keys you don't provide, eg giving a single key will keep all other keys to their actual value.作为旁注,
patchValue
应该保留您未提供的键的值,例如,提供单个键将使所有其他键保持其实际值。
Here is a stackblitz : https://stackblitz.com/edit/angular-xlxcic?file=src%2Fapp%2Fapp.component.ts这是一个堆栈闪电战: https ://stackblitz.com/edit/angular-xlxcic ? file = src%2Fapp%2Fapp.component.ts
constructor() {
const form = new FormGroup({
name: new FormControl(''),
surname: new FormControl('')
});
form.patchValue({
...form.value,
name: 'name of the user'
});
form.patchValue({
surname: 'surname of the user, with name kept'
});
console.log(form.value);
}
try this, No need to use loop试试这个,不需要使用循环
this.form.patchValue(model)
For information visit, https://dev.to/crazedvic/using-patchvalue-on-formarrays-in-angular-7-2c8c有关信息,请访问https://dev.to/crazedvic/using-patchvalue-on-formarrays-in-angular-7-2c8c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.