如何在 Angular 中使用多个组件初始化动态表单

发布于 2025-01-11 04:20:06 字数 122 浏览 2 评论 0原文

我想要一份包含员工轮班的表格。在 UI 中,我有一个表单来指定每天的班次,并且可以动态添加班次。

我希望我的班次组件负责创建单个班次 FormGroup。我正在努力初始化我的父表单,或者我不知道在哪个组件中初始化它。

I want to have a form which contains shift for an employee. In the UI, I have a form to specify a shift for each day and shifts can be dynamically added.

I want my shift component to be responsible for creating a single shift FormGroup. I am struggling to initialise my parent form or I don't know which component to initialise it in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

幸福%小乖 2025-01-18 04:20:06

您可以在父组件(即 Working day)上创建 shifts 表单数组,然后将各个数组控件作为 FormGroup 传递给子 shift 组件。

this.parentform = this.formBuilder.group({
      name: new FormControl(),
      shifts: new FormArray([]),
    });

---- Template ----
<app-shift [form]="parentform.get('shifts').controls[index]"></app-shift>

通过将 FormGroup 传递给子组件,您也可以在父组件上访问其值/验证器。

注意:正如您所提到的,班次将来自 API,您需要在响应中迭代班次数组并返回结果。使用 startDate/endDate 属性创建 FormGroup

它的工作原理如下:

Shift component:将 FormGroup 作为输入。

@Input() form: FormGroup;

  get shiftForm() {
    return this.form as FormGroup;
  }

在父组件中,创建表单组&迭代 shift FormArray.controls

Parent HTML

  <div formArrayName="shifts">
    <ng-container *ngFor="let shiftForm of shiftsArray.controls; let i = index">
      <app-shift [form]="shiftForm"></app-shift>
    </ng-container>
  </div>

TS:单击按钮/接收 API 响应时,调用 addShift() 方法添加新的 FormGroupshifts 控制中

  addShift() {
    const shiftForm = this.formBuilder.group({
      startDate: new FormControl(''),
      endDate: new FormControl(''),
    });
    this.shiftsArray.push(shiftForm);
  }

注意: Angular 的最新版本对类型转换非常严格。因此,您可能需要在传递给子级之前将每个 shiftForm 类型转换为父级中的 FormGroup。

这是一个有效的 stackblitz演示将 FormGroup 从父级传递给子级。

You can create the shifts form array on the parent i.e. Working day component and then pass the individual array controls as a FormGroup to the child shift component.

this.parentform = this.formBuilder.group({
      name: new FormControl(),
      shifts: new FormArray([]),
    });

---- Template ----
<app-shift [form]="parentform.get('shifts').controls[index]"></app-shift>

By passing FormGroup to the child component, you can access its values/validators on the parent also.

Note: As you have mentioned, that shifts will be coming from API, you would need to iterate over the shifts array in response & create FormGroup with startDate/endDate properties.

Here is how it would work:

Shift component: Take FormGroup as an input.

@Input() form: FormGroup;

  get shiftForm() {
    return this.form as FormGroup;
  }

In the parent component, create the form group & iterate over the shift FormArray.controls.

Parent HTML:

  <div formArrayName="shifts">
    <ng-container *ngFor="let shiftForm of shiftsArray.controls; let i = index">
      <app-shift [form]="shiftForm"></app-shift>
    </ng-container>
  </div>

TS: On button click/ receiving API response, call addShift() method to add a new FormGroup in shifts control

  addShift() {
    const shiftForm = this.formBuilder.group({
      startDate: new FormControl(''),
      endDate: new FormControl(''),
    });
    this.shiftsArray.push(shiftForm);
  }

Note: Recent versions of Angular are strict towards typecasting. Therefore you may need to typecast each shiftForm as FormGroup in the parent before passing to the child.

Here is a working stackblitz to demonstrate passing FormGroup from parent to child.

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