【问题标题】:remove form control dynamically in angular 2 reactive forms以 Angular 2 反应形式动态删除表单控件
【发布时间】:2017-11-27 15:34:18
【问题描述】:

尝试删除我添加到表单的控件

我尝试过this.*FormArray*.splice,但它无法编译。说FormArray has no method splice。然后我尝试了removeAt(),但是当我运行代码时,错误提示为removeAt is not a method。一次编译失败,下一次运行时失败。

我想从控件数组中删除一个控件。

【问题讨论】:

  • 如果你想从FormArray 中删除特定的FormGroup,你可以这样做(指定索引):removeAt(<index>)。如果要从每个 FormGroup 中删除特定控件,则必须遍历 FormArray。
  • @developer033 `[FormGroup, FormGroup] - 什么是 removeAt 被调用?
  • See this tutorial。正如我所说,它有一个 PLUNKER,你可以看到如何删除它。
  • 这应该写成答案:this.*FormArray*.removeAt()
  • 在动态删除表单控件后,我在提交表单时收到错误“TypeError: Cannot read property 'updateOn' of null”。有没有人解决这个问题? @菲尔

标签: angular angular2-forms angular-reactive-forms


【解决方案1】:

removeAt() 是 FormArray 命名空间上的方法,但不是 ES5-7 方法。

(<FormArray>items).removeAt(idx) 强制数组成为 Typescript FormArray 对象,从而打开了方法。

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2016-09-18
    相关资源
    最近更新 更多