微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在不发出 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 });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。