简体   繁体   English

角反应形式:垫选择的FormArray

[英]Angular Reactive Forms: FormArray of mat-select

I need to create a Form with a FormArray of mat-select controls (CoordinatorX) as displayed in below Pic: 我需要创建一个带有mat-select控件(CoordinatorX)的FormArray的Form,如下图所示:

在此处输入图片说明

The code that produce the above Form is as follows: 产生以上表格的代码如下:

component.html: 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: 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           :   ''
    })]),
});

Upto this point, it works well. 到目前为止,它运行良好。 I am able to select the options from any of the 3 mat-select controls. 我可以从3个mat-select控件中mat-select任何一个。

But I try adding another coordinator dynamically by the following code: 但是我尝试通过以下代码动态添加另一个协调器:

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

It adds a new control as expected. 它按预期添加了新控件。 But when I click on it, it is not showing the options. 但是当我单击它时,它没有显示选项。 Any further dynamically added mat-select controls are not working right. 任何进一步动态添加的mat-select控件均无法正常工作。

Any help please? 有什么帮助吗?

PS: I am using Angular material controls PS:我正在使用Angular材质控件

我认为也许您需要使您的arrayFormControl反应

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应形式formarray angular中使用mat-select - How to use mat-select with reactive forms formarray angular 通过formarray进行Mat-select不会显示选定的值和选项-响应式Angular - Mat-select by formarray not showing selected value and options - Reactive forms Angular Angular FormArray和formGroup中的材质mat-select - Angular Material mat-select in FormArray and formGroup 如何在 angular 中使用表单组的表单数组进行 m​​at-select - how to use formarray of formgroups for mat-select in angular Angular Material反应形式,根据其他选择更新垫选择选项 - Angular Material reactive forms, updating mat-select options based on other select Angular Material:使用反应形式为对象选择垫选择多个默认值 - Angular Material: mat-select multiple default value for object selection using reactive forms Angular Material:使用反应形式时垫选择默认值 - Angular Material: mat-select default value when using reactive forms Angular 材料:使用反应式 forms 从服务中选择设置值数据 - Angular Material: mat-select set value data from service using reactive forms mat-select 动态反应形式中的更新选项 - Update options in mat-select dynamic reactive forms Angular 反应形式补丁值不适用于 mat-select 下拉菜单 - Angular reactive form patchvalue not working with mat-select dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM