如何在Angular 8中编辑动态创建的形式的数据
我是 Angular 开发新手。在我的项目中,我有一个表单,可以使用 Angular 8 应用程序中的“添加和删除”按钮动态添加和删除输入框。提交表单后,我想使用相同的添加和删除按钮显示和编辑输入字段。我怎样才能实现编辑功能。
我分享了使用添加和删除按钮创建动态表单的 stackblitz url。我需要相同的表单进行编辑。谁能帮我提供解决方案? https://stackblitz。 com/edit/angular-dynamic-form-fields-y2ijvu?file=src%2Fapp%2Fapp.component.ts
I am new to Angular developmet.In my project,I have a form with dynamically add and remove input box using Add and Remove button in Angular 8 Application. After submitting the form,i want to display and edit the input fields with the same add and remove button. How can i achieve the functionality in edit.
I share the stackblitz url of creating a dynamic form with add and remove button.I need the same form for edit. Can anyone help me to give solution?
https://stackblitz.com/edit/angular-dynamic-form-fields-y2ijvu?file=src%2Fapp%2Fapp.component.ts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使用动态接收的值(例如从 api)填充手机,您需要向
addPhone()
添加一个参数,并在迭代接收的值时调用它。receivedPhones.forEach(phone => this.addPhone(phone))
或者您可以使用
.setValue()
或.patchValue()
FormArray 如下所示:this.userForm.get('phones').setValue(receivedPhones)
数组具有相等的成员计数)
(
setValue()
要求 可以共享一个表单用于编辑和查看情况,只需添加一个标志isNew
并填充它,然后使用它。通常,编辑功能驻留在不同的路由 (.../edit) 中,这使您能够在路由的data
定义上提供isNew: true
。In order to populate the phones with dynamically received values (eg from api), you would need to add a parameter to
addPhone()
and call it when iterating over the received values.receivedPhones.forEach(phone => this.addPhone(phone))
or you could
.setValue()
or.patchValue()
of the FormArray like so:this.userForm.get('phones').setValue(receivedPhones)
(
setValue()
requires that the array has equal member count)It is possible to share one form for edit and view situations, just add a flag
isNew
and populate it, and use it. Oftentimes, the edit functionality resides at a different route (.../edit) which enables you to provideisNew: true
on the route'sdata
definition.