gpt4 book ai didi

forms - Angular 2 : formGroup expects a FormGroup instance. 请传入一个

转载 作者:太空狗 更新时间:2023-10-29 16:48:41 26 4
gpt4 key购买 nike

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

EXCEPTION: Uncaught (in promise): Error: Error in ./EditPatientComponent class EditPatientComponent - inline template:1:10 caused by: formGroup expects a FormGroup instance. Please pass one in.

这是有错误的当前代码。

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>

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)
})
};

}

任何帮助/指向正确方向的帮助都会很棒!谢谢!

最佳答案

您的 patientFormundefined 直到订阅中的 patient 被填充。因此,您试图绑定(bind)到一个在解析模板时模板中不存在的值。

添加一个 *ngIf 以仅在 patient 为真或实例化表单组时呈现表单:

<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>

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

表单并不总是有起始值,因此您还可以检查表单本身是否存在:

<form *ngIf="patientForm" [formGroup]="patientForm" (ngSubmit)="onSubmit()">

重要的是表单在实例化之前不会呈现。

关于forms - Angular 2 : formGroup expects a FormGroup instance. 请传入一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com