问题描述
有这样的形式:
this.form = this.fb.group({
name: [null,[Validators.required]],companyHolderUid: [null],generalDescription: [null],authority: [TemplateAuthorities.Root],tabs: this.fb.array([]),tagReferences: this.fb.array([]),},{ updateOn: 'blur' });
我想在不触发 valueChanges 的情况下更新 tabs 或 tagReferences。像这样的东西
{ emitEvent: false,onlySelf: true }
当使用 patchValue 并试图控制 tabs 为空时
private formInit(template: AssetTemplate) {
this.form.patchValue({
name: template.name,companyHolderUid: template.companyHolderUid,generalDescription: template.generalDescription,authority: template.authority,tabs: [1,2,3,4]
},{ emitEvent: false,onlySelf: true });
console.log(JSON.stringify(this.form.value));
}
console.log 结果:
{
"name":"test template","companyHolderUid":null,"generalDescription":"Lorem ipsum","authority":"ROOT","tabs":[],"tagReferences":[]
}
解决方法
使用 patchValue 方法。
您可以传递整个表单值,或者仅在需要时更新特定字段。
const newValue = [];
this.form.patchValue({ tabs: newValue },{ emitEvent: false });