Angular 2:formGroup需要一个FormGroup实例.请通过一个

Jes*_*Sue 35 forms angular

我正在Angular 2中创建一个表单.我的目标是从API获取数据并将其传递到表单中以进行编辑.但是,我遇到了这个错误:

EXCEPTION:未捕获(在承诺中):错误:./EditPatientComponent类中的错误EditPatientComponent - 内联模板:1:10引起:formGroup需要一个FormGroup实例.请通过一个.

这是带错误的当前代码.

HTML

<section class="CreatePatient">
    <form [formGroup]="patientForm" (ngSubmit)="onSubmit()">
        <div class="row">
            <div class="form-group col-12 col-lg-3">
                <label for="firstName">First Name</label>
                <input formControlName="firstName" type="text" class="form-control" id="firstName" >
            </div>

        <div class="row">
            <div class="col-12 col-lg-2">
                <button type="submit" name="submit" class="btn btn-block btn-primary">Save</button>
            </div>
        </div>
    </form>
</section>
Run Code Online (Sandbox Code Playgroud)

TS

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

import { PatientService } from './patient.service';
import { Patient } from './patient';

@Component({
    templateUrl: 'editpatient.component.html'
})
export class EditPatientComponent implements OnInit {
    errorMessage: string;
    id: string;
    editMode = true;
    private patientForm: FormGroup;
    private patient: Patient;

    constructor(
        private patientService: PatientService,
        private router: Router,
        private activatedRoute: ActivatedRoute,
        private formBuilder: FormBuilder) {

        console.log("routes");
        console.log(activatedRoute.snapshot.url[1].path);
    }

    ngOnInit() {
        this.getPatient();
    }

    getPatient() {
            this.patientService.getPatient(this.activatedRoute.snapshot.url[1].path)
            .subscribe(
                patient => {
                    this.id = this.activatedRoute.snapshot.url[1].path;
                    this.patient = patient;
                    this.initForm();
                },
                error =>  this.errorMessage = <any>error);

    }

    onSubmit(form){
        console.log(this.patientForm);
        // Post the API
    };

    initForm() {
        let patientFirstName = '';

        if (this.editMode) {
            console.log(this.patient.firstName);
            console.log(this.patient.lastName);
            console.log(this.patient.participantUuid);
            patientFirstName = this.patient.firstName;
        }

        this.patientForm = new FormGroup({
            'firstName': new FormControl(patientFirstName)
        })
    };

}
Run Code Online (Sandbox Code Playgroud)

任何帮助/指向我正确的方向将是伟大的!谢谢!

Bra*_*don 78

patientFormundefined直到patient在订阅被填充.因此,您尝试绑定到解析模板时模板中不存在的值.

*ngIf仅在患者处于真实状态时添加以呈现表单:

<section class="CreatePatient">
    <form *ngIf="patient" [formGroup]="patientForm" (ngSubmit)="onSubmit()">
        <div class="row">
            <div class="form-group col-12 col-lg-3">
                <label for="firstName">First Name</label>
                <input formControlName="firstName" type="text" class="form-control" id="firstName" >
            </div>

        <div class="row">
            <div class="col-12 col-lg-2">
                <button type="submit" name="submit" class="btn btn-block btn-primary">Save</button>
            </div>
        </div>
    </form>
</section>
Run Code Online (Sandbox Code Playgroud)

在订阅中填充患者时,patientForm实例将存在并且绑定将起作用.处理异步值时,这是一个常见的"陷阱".


Vol*_*hat 15

问题是您的表单在开头是空的.

只有在初始化时,您才会耐心等待,然后再创建它.你应该在开始或初始化你的表格

<section class="CreatePatient" *ngIf="patientForm">
Run Code Online (Sandbox Code Playgroud)

  • 我认为这一定是解决方案.因为每个表单都没有值upfront.eg`login form` (3认同)