错误TypeError:this.validator不是函数FormArray

时间:2019-06-28 14:04:56

标签: angular typescript angular-reactive-forms

我有一个错误,只有在控件位于formArray内时才会发生,我有一个mat-select来选择一周中的几天,正是由于这个控件,我才收到以下错误。

我在做错什么导致该错误发生?

vendor.js:65387 ERROR TypeError: this.validator is not a function
    at FormControl._runValidator (vendor.js:106921)
    at FormControl.updateValueAndValidity (vendor.js:106882)
    at new FormControl (vendor.js:107326)
    at FormBuilder.control (vendor.js:111089)
    at FormBuilder._createControl (vendor.js:111149)
    at vendor.js:111128
    at Array.forEach (<anonymous>)
    at FormBuilder._reduceControls (vendor.js:111123)
    at FormBuilder.group (vendor.js:111041)
    at :4200/admin-admin-module.js:7871

element中的对象professionals.forEach中的返回值是;

  element: {
        daysService: [2, 3],
        endService: "",
        id: "45ySYbmImayrgv7teqWr",
        startService: "",
        name: "Name professional",
        rooms: [],
    };

form.component.ts:

  this.weekDays = [
    { label: 'Sunday', value: 0 },
    { label: 'Monday', value: 1 },
    { label: 'Tuesday', value: 2 },
    { label: 'Wednesday', value: 3 },
    { label: 'Thursday', value: 4 },
    { label: 'Friday', value: 5 },
    { label: 'Saturday', value: 6 },
  ];

this.professionals
  .pipe(takeUntil(this._unsubscribeAll))
  .subscribe(professionals => {
    if (!professionals) return;
    this.setProfessionalsUnity(professionals);
  });



 private _buildForm(): void {
    this.form = this._formBuilder.group({
      professionals: this._formBuilder.array([])
    });
  }

  get professionalsArray() {
    return <FormArray>this.form.get('professionals');
  }

  public setProfessionalsUnity(professionals): void {
    this.professionalsArray.clear();
    let control = this.professionalsArray;
    if (!professionals) return;
    professionals.forEach(element => {
      // I believe the error is happening because of this line ...
      control.push(this._formBuilder.group(element));
    });
  }

 public addGroupProfessional() {
    return this._formBuilder.group({
      id: [''],
      name: [''],
      rooms: [''],
      daysService: [''],
      startService: [''],
      endService: ['']
    });
  }

form.html:

<form [formGroup]="form">

  <mat-accordion formArrayName="professionals">
    <mat-expansion-panel *ngFor="let group of professionalArray.controls; let i = index" [formGroupName]="i"
      [expanded]="indexExpanded == i && newProfessional">
      <mat-expansion-panel-header>
        <mat-panel-title>
          <mat-icon class="secondary-text mr-12">person</mat-icon>
          {{ group.get('name').value }}
        </mat-panel-title>
        <mat-panel-description>
        </mat-panel-description>
      </mat-expansion-panel-header>

      <div fxLayout="row" fxLayoutAlign="start">
        <mat-form-field fxFlex="50">
          <input matInput placeholder="Name of professional..." formControlName="name">
        </mat-form-field>
      </div>

      <div class="h3 blue-fg mb-12">Attendance</div>

      <div fxLayout="row" fxLayout.xs="column" fxLayoutAlign="start">
        <mat-form-field fxFlex="50">
          <mat-select placeholder="Days you attend" formControlName="daysService" multiple>
            <mat-option *ngFor="let day of weekDays" [value]="day.value">{{day.label}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <mat-action-row>
        <button mat-button color="warn" (click)="removeProfessional(i)" *ngIf="group.value.id">Delete</button>
        <button mat-button color="accent" [disabled]="group.value.name.length < 5" *ngIf="group.value.id"
          (click)="updateProfessional(i)">Save</button>
        <button mat-button color="accent" [disabled]="group.value.name.length < 5" *ngIf="!group.value.id"
          (click)="addProfessional(i)">Add</button>
      </mat-action-row>
    </mat-expansion-panel>
  </mat-accordion>

</form>

2 个答案:

答案 0 :(得分:1)

以这种方式尝试:

    daysService: [[2, 3]]

答案 1 :(得分:0)

您需要包装数组。对于现有对象,您可以这样做:

echo off
copy "Z:\Backup\SQLBackup\" "C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\DailyStep_bck.bak"