【问题标题】:How do I update the values of formArray from a component in Angular 6如何从 Angular 6 中的组件更新 formArray 的值
【发布时间】:2018-10-20 20:13:09
【问题描述】:

大家好,请问我在从我的 angular 6 组件更新 formArray 中的值时遇到问题。我试过使用 setValue() 和 patchValue() 没有一个在工作。下面是我的代码

 this.form = this._formBuilder.group({
       locationRequests: this._formBuilder.array([
          this._formBuilder.group({
          location: '',
          item_category: '',
          items: '',
          quantity: '',
          packaging: '',
          unit_of_measurement: '',
    });
        ])
    });

我想为组件中的每个字段设置值,我已经尝试过

this.form.patchValue({
 locationRequests: this._formBuilder.array([
          this._formBuilder.group({
          location: 'New York',
          item_category: 2,
          items: 1,
          quantity: 500,
          packaging: 1,
          unit_of_measurement: 'PCS',
}
});

我也试过用 setValue 函数还是不行。我在网上搜索过,所有答案都不起作用。

【问题讨论】:

    标签: javascript angular


    【解决方案1】:

    修补时使用这个。

    this.form.patchValue({ locationRequests: [ { location: 'New York', item_category: 2, items: 1, quantity: 500, packaging: 1, unit_of_measurement: 'PCS', }] });

    在修补值时,您无需指定 this._formBuilder.groupthis._formBuilder.array

    【讨论】:

    • 非常感谢@sachinGupta 的回答,在过去的 24 小时里我一直在与这个问题作斗争。我刚试了你的代码,效果很好。
    猜你喜欢
    • 2021-06-15
    • 2021-01-21
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2019-02-04
    • 2018-08-14
    相关资源
    最近更新 更多