[英]Set nested form group/control values in reactive forms
我的应用程序中有一个Reactive Form
,其结构如下:
this.bioForm = this._fb.group({
firstName: [this.user.firstName, [Validators.required]],
experience: this._fb.group({
field0: ['', [Validators.required, Validators.maxLength(500)]],
field1: ['', [Validators.required, Validators.maxLength(500)]],
field2: ['', [Validators.required, Validators.maxLength(500)]],
field3: ['', [Validators.required, Validators.maxLength(500)]],
field4: ['', [Validators.maxLength(500)]],
field5: ['', [Validators.maxLength(500)]]
}),
skills: this._fb.array([], Validators.required),
});
在我的浏览器local storage
我具有以下内容:
我尝试过的
// Get Value from LOCAL STORAGE
let localStorage_experience = JSON.parse(localStorage.getItem("experience")) || "";
// Set nested form controls value
this.bioForm.setValue({experience:{field0: localStorage_experience.field0 || ""}});
我正在尝试从本地存储中检索值,并针对嵌套的表单控件值进行设置。
您可以使用setControl
替换现有的formcontrol / formgroup / formarray:
this.bioForm.setControl('experience', this._fb.group(localStorage_experience));
或者您可以使用patchValue
:
this.bioForm.patchValue({experience: localStorage_experience});
如果要使用setValue
,则需要将其应用于嵌套表单组,因为setValue
希望设置所有值。
this.bioForm.controls.experience.setValue(localStorage_experience);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.