在反应形式中创建形式控制组

时间:2019-12-23 11:03:12

标签: angular reactive-forms

试图为响应式表单创建html,但是我总是遇到错误

这是我的表格

 private buildForm(): void {
    this.formOperational = this.formBuilder.group({
      comment: [this.expertOperational.comment],
      workingDays: new FormArray([])
    });
  }

  private createWorkignDays(): void {
    this.operational.workingDay.forEach((day: WorkingDay) => {
      this.workHoursControls.push(
        this.formBuilder.group({
          weekDay: [day],
          operationalAM: this.formBuilder.group({
            fixed: [day.operationalAM.fixed],
            startTime: [day.operationalAM.startTime],
            endTime: [day.operationalAM.endTime]
          }),
          operationalPM: this.formBuilder.group({
            fixed: [day.operationalAM.fixed],
            startTime: [day.operationalAM.startTime],
            endTime: [day.operationalAM.endTime]
          })
        })
      );
    });
  }

当我使用HTML时,我会尝试类似的操作

<form [formGroup]="formOperational">
    <div class="row pb-1" *ngFor="let day of formOperational.get('workingDays').controls; let i = index;">
        <div class="col-lg-2 col-sm-2 col-md-2 justify-content-center align-self-center">
            {{'Shared.WeekDay.' + day.weekDay | translate }}
        </div>
        <div class="row" [formGroup]="i">
            <div class="text-center col">
                <input type="checkbox" formControlName="fixed">
            </div>
        </div>
        <div class="col">
            <input class="form-control text-center width-70" formControlName="startTime" />
        </div>
        <div class="col">
            <input class="form-control text-center width-70" formControlName="endTime" />
        </div>
    </div>
    <div class="form-group">
        <textarea class="form-control rounded-0" formControlName="comment"
            rows="11">{{expertOperational.comment}}</textarea>
    </div>
</form>

我遇到错误

ERROR Error: formGroup expects a FormGroup instance. Please pass one in.

       Example:


    <div [formGroup]="myGroup">
      <input formControlName="firstName">
    </div>

    In your class:

    this.myGroup = new FormGroup({
       firstName: new FormControl()
    });
    at Function.push../node_modules/@angular/forms/fesm5/forms.js.ReactiveErrors.missingFormException (forms.js:1443)
    at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective._checkFormPresent (forms.js:5414)
    at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.ngOnChanges (forms.js:5237)
    at checkAndUpdateDirectiveInline (core.js:22095)
    at checkAndUpdateNodeInline (core.js:23363)
    at checkAndUpdateNode (core.js:23325)
    at debugCheckAndUpdateNode (core.js:23959)
    at debugCheckDirectivesFn (core.js:23919)
    at Object.eval [as updateDirectives] (ExpertOperationalComponent.html:6)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)

有人知道我错在哪里吗? 谢谢

2 个答案:

答案 0 :(得分:1)

您说您的HTML不好。您在那里遇到错误吗? 试试这个
删除

NSPredicate(format:"ANY genres.title == %@", chosenGenreTitle)

添加
设置吸气剂

*ngFor="let day of formOperational.get('workingDays').controls; let i = index;"

然后在模板中

get controls() {
  return (this.formOperational.get('workingDays') as FormArray).controls;
}

答案 1 :(得分:1)

您应该声明一个formOperational: FormGroup;之类的FormGroup。在您的comonent.ts中尝试一下,

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup} from '@angular/forms';


@Component({
  selector: 'app-example',
  templateUrl: './app-example.component.html',
  styleUrls: ['./app-example.component.scss']
})

export class AppExampleComponent implements OnInit {

  formOperational: FormGroup;

    constructor(
    private _formBuilder: FormBuilder,
  ) {}

}