【问题标题】:Angular FormArray - Cannot read property 'push' of nullAngular FormArray - 无法读取 null 的属性“推送”
【发布时间】:2021-01-19 13:32:46
【问题描述】:

我正在使用 Angular Reactive FormArray 在添加按钮上添加多个输入。

我的设置中有多个 formGroup。当我尝试添加/推送输入时,出现“无法读取属性‘push’ of null”的错误。

我的 formGroup 设置有什么问题还是因为 formArray 中有多个 FormGroups

我的代码:HTML

<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>

角度代码:

form: FormGroup;

constructor(private fb: FormBuilder) {
    this.form = this.fb.group({
      published: true,
      formArray: this.fb.array([
        this.fb.group({
          credentials: this.fb.array([]),
        })
      ])
    });
}

addCreds() {
    const creds = this.form.get('credentials') as FormArray;
    creds.push(this.fb.group({
      username: '',
      password: '',
    }));
}

我也有https://stackblitz.com/edit/angular-form-array-example-hfmrm2

【问题讨论】:

    标签: angular formarray


    【解决方案1】:

    由于您已经嵌套了 formArray,因此您必须按如下方式更改模板。

    component.html

     <ng-container
          formArrayName="formArray"
          *ngFor="let forArr of form.get('formArray').controls; let x = index"
        >
          <ng-container [formGroupName]="x">
            <div
              formArrayName="credentials"
              *ngFor="
                let creds of forArr.controls.credentials?.controls;
                let i = index
              "
            >
              <ng-container [formGroupName]="i">
                <input placeholder="Username" formControlName="username" />
                <input placeholder="Password" formControlName="password" />
              </ng-container>
            </div>
          </ng-container>
        </ng-container>
    

    component.ts

     addCreds() {
        const formArray = this.form.get("formArray") as FormArray;
        (formArray.controls[0].get("credentials") as FormArray).push(
          this.fb.group({
            username: "",
            password: ""
          })
        );
      }
    

    Forked Example

    【讨论】:

    • 嘿,谢谢,但我有多个 formGroups,例如,这是我使用 Angular Material Stepper 作为 IM 的主要 formArray:this.resumeForm = this.fb.group({ formArray: this.fb.array([ this.fb.group({ jobName: ['', Validators.required], }), this.fb.group({ firstName: ['', Validators.required], lastName: ['', Validators.required], }), this.fb.group({ facebook : ['', Validators.required], youtube: ['', Validators.required], }), this.fb.group({ credentials: this.fb.array( []) }), ])})
    • 请告诉我,因为我几个小时以来一直在努力解决这个问题
    • 为什么你在 formArray 中有不同命名的 formGroup?
    • 我不想创建多个 formGroup 必须检查和检查每个表单提交:material.angular.io/components/stepper/overview
    • 嘿,伙计,这对我有用,非常感谢
    猜你喜欢
    • 2020-05-23
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2015-04-06
    相关资源
    最近更新 更多