如何在Angular 8中编辑动态创建的形式的数据

发布于 2025-01-18 15:16:26 字数 423 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2025-01-25 15:16:26

为了使用动态接收的值(例如从 api)填充手机,您需要向 addPhone() 添加一个参数,并在迭代接收的值时调用它。

  addPhone(phone: string): void {
    (this.userForm.get('phones') as FormArray).push(phone);
  }

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.

  addPhone(phone: string): void {
    (this.userForm.get('phones') as FormArray).push(phone);
  }

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 provide isNew: true on the route's data definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文