【问题标题】:How to dynamically add formgroup with formcontrols to formgroup如何将带有表单控件的表单组动态添加到表单组
【发布时间】:2019-08-22 13:34:36
【问题描述】:

我有一个非常大的表单,其中包含许多复选框、单选框、文本区域... 整个表单是从数据库动态生成的。

在某些时候,我需要验证一组复选框,其中必须选中该复选框组的至少一个复选框。 所以我遵循了这本手册:https://stackoverflow.com/a/54036689/11937089

我的问题是,表单组里面的表单组必须是动态生成的。

我尝试过这样的事情:

this.outerForm.addControl('checkboxGroup' + (foo.id + ' ')), new FormGroup({}));

foo.forEach(bar => { get('checkboxGroup' + (foo.id + ' ')).addControl('checkbox'+ (foo.id + ' ') + (bar.id + ' ')), new FormControl(' ')); }

但我很确定这是完全错误的,因为 addControl 不适用于表单组,而且我不知道如何在 addControl-Part 中引用动态 FormGroup checkboxGroup1,get 似乎是错误的。

outerform
--> textarea
--> checkboxGroup1
----->checkbox11
----->checkbox12
----->checkbox13
----->checkbox14
--> textarea
--> textarea
--> checkboxGroup2
----->checkbox21
----->checkbox22
----->checkbox23
----->checkbox24

【问题讨论】:

标签: javascript angular


【解决方案1】:

自己解决了,我真的很接近。 this.outerForm.addControl('checkboxGroup' + (foo.id + ' ')), new FormGroup({})); 工作得很好。

get('checkboxGroup' + (foo.id + ' ')).addControl('checkbox'+ (foo.id + ' ') + (bar.id + ' ')), new FormControl(' ')); 不完整。

(this.outerForm.get('checkboxGroup' + (foo.id + ' ')) as any).addControl('checkbox'+ (foo.id + ' ') + (bar.id + ' ')), new FormControl(' ')); 成功了。

【讨论】:

    【解决方案2】:

    你可以使用@angular/forms。因此,您可以使用 FormGroup、FormArray、FormControl。 举个例子,请看https://stackblitz.com/edit/angular-3x3tme?file=src/app/hello.component.ts

    【讨论】:

      【解决方案3】:

      用 formArray 试试这样的东西 https://stackblitz.com/edit/angular-form-array-example

      import { Component } from '@angular/core';
      import { FormControl, FormGroup, FormArray, FormBuilder } from '@angular/forms';
      
      @Component({
        selector: 'my-app',
        template: `
          <form [formGroup]="form">
            <input type="checkbox" formControlName="published"> Published
            <div *ngIf="form.controls.published.value">
      
              <h2>Credentials</h2>
              <button (click)="addCreds()">Add</button>
      
              <div formArrayName="credentials" *ngFor="let creds of form.controls.credentials?.value; let i = index">
                <ng-container [formGroupName]="i">
                  <input placeholder="Username" formControlName="username">
                  <input placeholder="Password" formControlName="password">
                </ng-container>
              </div>
      
            </div>
          </form>
        `,
      })
      export class AppComponent  {
        form: FormGroup;
      
        constructor(private fb: FormBuilder) {
          this.form = this.fb.group({
            published: true,
            credentials: this.fb.array([]),
          });
        }
      
        addCreds() {
          const creds = this.form.controls.credentials as FormArray;
          creds.push(this.fb.group({
            username: '',
            password: '',
          }));
        }
      }
      

      【讨论】:

      • 请在您的回答中展示建议的解决方案。链接不稳定。
      猜你喜欢
      • 2017-08-31
      • 1970-01-01
      • 2017-03-05
      • 2011-06-04
      • 2020-09-09
      • 2014-01-27
      • 2020-06-11
      • 2018-11-27
      • 2020-01-22
      相关资源
      最近更新 更多