在不发出 valueChanges 的情况下更新 formArray

问题描述

有这样的形式:

    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 的情况下更新 tabstagReferences。像这样的东西 { 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 });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...