【问题标题】:Angular Reactive Forms: FormArray of mat-selectAngular Reactive Forms:mat-select 的 FormArray
【发布时间】:2018-12-22 19:13:22
【问题描述】:

我需要创建一个 FormArray 的 mat-select 控件 (CoordinatorX),如下图所示:

产生上述表格的代码如下:

component.html:

<span formArrayName="coordinators" class="col-12" *ngIf="coordinators.controls.length > 0">
    <span class="row" *ngFor="let coord of coordinators.controls; let i = index" [formGroupName]="i">

        <mat-form-field class="col-10 col-md-11 col-lg-10">
            <mat-select formControlName="name" placeholder="Coordinator {{i}}">
                <mat-option [value]="admin.name" *ngFor="let admin of (cNgo$ | async)?.admin"> {{ admin.phone}} - {{ admin.name }} </mat-o
            </mat-select>
        </mat-form-field>

        <div class="col-2 col-md-1 col-lg-2">
            <button type="button" (click)="removeCoordinator(i)" mat-icon-button *ngIf="i > 0">
                <mat-icon aria-label="Delete">delete</mat-icon>
            </button>
        </div>

    </span>
</span>

component.ts:

return this.fb.group({
    name                :   ['', [
        Validators.required,
        Validators.minLength(v.name.minLength),
        Validators.maxLength(v.name.maxLength)
    ]],

    shortCode           :   ['', [
        Validators.required,
        Validators.minLength(v.shortCode.minLength),
        Validators.maxLength(v.shortCode.maxLength)
    ], [
        EventValidator.shortCodeValidator(that.http)
    ]],

    coordinators        :   this.fb.array([this.fb.group({
        id              :   '',
        name            :   '',
        phone           :   '',
        email           :   ''
    }), this.fb.group({
        id              :   '',
        name            :   '',
        phone           :   '',
        email           :   ''
    }), this.fb.group({
        id              :   '',
        name            :   '',
        phone           :   '',
        email           :   ''
    })]),
});

到目前为止,它运行良好。我可以从 3 个mat-select 控件中的任何一个中选择选项。

但我尝试通过以下代码动态添加另一个协调器:

addCoordinator() {
    this.coordinators.push(this.fb.group({
        id                  :   '',
        name                :   '',
        phone               :   '',
        email               :   ''
    }));
    console.log(this.eventForm);
}

它按预期添加了一个新控件。但是当我点击它时,它没有显示选项。任何进一步动态添加的mat-select 控件都无法正常工作。

有什么帮助吗?

PS:我正在使用 Angular 材质控件

【问题讨论】:

  • 什么是this.coordinators?
  • 这是一个Getter,定义为get coordinators(): FormArray { return this.eventForm.get('coordinators') as FormArray; };

标签: angular angular-material angular-reactive-forms formarray


【解决方案1】:

我想也许你需要让你的 arrayFormControl 具有响应性

<span [formArrayName]="coordinators"...>

【讨论】:

  • 这样做会引发几个错误:(1) 错误错误:在 setUpFormContainer (forms.js:1573) 的 _throwError (forms.js:1591) 处找不到名称为“[object Object]”的控件) (2) EventCreateComponent.html:35 ERROR 错误: 在 setUpFormContainer (forms.js:1573) 的 _throwError (forms.js:1591) 处找不到路径的控件: '[object Object] -> 0' (3) ERROR 错误: 找不到带有路径的控件:'[object Object] -> 0 -> name'
猜你喜欢
  • 2020-07-24
  • 2019-01-31
  • 2018-12-22
  • 1970-01-01
  • 2022-12-27
  • 2018-10-13
  • 1970-01-01
  • 2023-03-07
  • 2021-06-04
相关资源
最近更新 更多