首页 文章

使用Angular 5 Form Group和Array更新数据

提问于
浏览
1

我使用Angular FormGroup和FormArray允许用户添加FormGroup的新实例并编辑当前表单 . 我无法弄清楚如何更新数据 . 看到这里.. https://stackblitz.com/edit/form-array-patch-jfb9f8?file=app/app.component.html

我可以在单击"Add"按钮时追加表单的新实例,但是在保存数据时(无论是新表单还是添加的表单), updateBins() 函数应该如何工作 . 苦苦挣扎如何将两者联系在一起 .

1 回答

  • 1

    你几乎就在那里,只需要做一些改变:

    1)将FormGroup推送到FormArray

    this.binsFormArray.controls.push(this.createBin(newBin));
    

    应该

    this.binsFormArray.push(this.createBin(newBin));
    

    2)显示表格值

    <pre>{{ bins | json }}</pre>
    

    应该

    <pre>{{ binsForm.value | json }}</pre>
    

    enter image description here

相关问题