【问题标题】:Update formArray without valueChanges emiting更新 formArray 而不发出 valueChanges
【发布时间】:2021-04-15 00:35:51
【问题描述】:

有这样的形式:

    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":[]
}

【问题讨论】:

    标签: angular angular-reactive-forms formarray


    【解决方案1】:

    使用patchValue 方法。
    您可以传递整个表单值,或者仅在您愿意时才更新特定字段。

    const newValue = [];
    this.form.patchValue({ tabs: newValue }, { emitEvent: false });
    

    【讨论】:

    • 使用类似的东西,但是在尝试控制台时 - 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 }); // this.form.setControl('tagReferences', this.fb.array(this.template.tagReferences || [])); // this.form.setControl('tabs', this.fb.array(this.template.tabs)); } ```
    • 你能更新你的问题并发布你的整个代码吗?
    • 我的意思是,你的整个代码,包括console.log
    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 2019-05-08
    • 2023-02-05
    • 2020-02-26
    • 2019-02-23
    • 2020-11-10
    • 2019-07-23
    • 2019-11-12
    相关资源
    最近更新 更多