【问题标题】:Update value in form array using patchValue [closed]使用 patchValue 更新表单数组中的值 [关闭]
【发布时间】:2021-08-13 16:22:14
【问题描述】:

我正在尝试使用 patchValue 更新 formArray 中的值,但在某些情况下,例如:

表单数组:

0: {id: 12, start_time: "17:11", end_time: "17:41", name: "AUB Medical Center"}
1: {id: 15, start_time: "17:58", end_time: "18:03", name: "Sanayeh"}

我想更改对象 1 中的 start_time。

所以我需要使用这个:this.locationForm.patchValue(newValueOfStartTime);

这里我无法为 patchValue 分配我想要更改的对象的 id 和参数!

有什么建议吗?

【问题讨论】:

  • Angular 有 documentation 说明如何做到这一点。
  • 感谢文档。帮了我很多@R.Richards

标签: angular forms formarray


【解决方案1】:

赋值如下

this.locationForm[index].get('start_time').setValue(newValueOfStartTime);

【讨论】:

  • 那太好了,但是,如果我在数组中有 3 个对象,我该如何分配第二个对象? at(index) 做什么?
  • @MohamadAbbas 请在stackblitz分享代码
  • 只有我需要添加:this.locationForm.at(index).patchValue...
  • @MohamadAbbas 用另一种方法更新了解决方案
【解决方案2】:

它!

 this.locationForm.at(index).patchValue({
       start_time: newValueOfStartTime
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2019-04-06
    • 2022-07-26
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多