[英]Angular form, value can't be changed after setValue
So I have a simple form.所以我有一个简单的表格。 The idea is to fill user values in the form so the user will have ability to modify it.这个想法是在表单中填写用户值,以便用户能够修改它。
<button mat-raised-button color="primary" (click)="OpenPatientEdit()">Edit</button>
<div *ngIf="isEdit">
<form [formGroup]="patientFormGroup" fxFlex="70">
<mat-form-field>
<mat-label>Name</mat-label>
<input matInput formControlName="name">
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Surname</mat-label>
<input matInput formControlName="surname">
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Address</mat-label>
<input matInput formControlName="address">
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Phone</mat-label>
<input matInput formControlName="phone">
</mat-form-field>
</form>
<button mat-raised-button color="primary" (click)="EditPatient()">Submit</button>
</div>
The problem is that after I change form values in the OpenEdit function, form values can not be changed from the html.问题是我在 OpenEdit function 中更改表单值后,无法从 html 更改表单值。
OpenPatientEdit() {
this.isEdit = !this.isEdit;
this.patientFormGroup.patchValue({
name: this.patient.name,
surname: this.patient.surname,
address: this.patient.address,
phone: this.patient.phone
});
}
After submit I always receive the values that were set from component but not on html (here null was set to all values of the form in OpenEdit function and I entered some random data from html):提交后,我总是收到从组件设置但不在 html 上设置的值(这里 null 设置为 OpenEdit function 中表单的所有值,我从 html 输入了一些随机数据:
But if I remove the form fill in the OpenEdit everything is ok:但是,如果我删除了 OpenEdit 中的表格,一切正常: So how should I fill the form data to give the user ability to change it?那么我应该如何填写表单数据以使用户能够更改它呢?
Component.ts:组件.ts:
import { Component, OnInit } from '@angular/core';
import { HomeDetails } from '../models/home.details.interface';
import { DashboardService } from '../services/dashboard.service';
import { UserTypeService } from '../../shared/services/user-type.service'
import { Doctor } from '../../shared/models/doctor'
import { PatientViewModel } from '../../shared/models/patient-view-model'
import { Subscription } from 'rxjs';
import { FormBuilder, FormControl, Validators } from '@angular/forms';
import { ErrorStateMatcher } from '@angular/material/core';
import { Patient } from '../../shared/models/patient'
import { UserService } from '../../shared/services/user.service'
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
private userTypeSubscription: Subscription;
userType = "";
doctor: Doctor;
patient: PatientViewModel;
isEdit = false;
patientFormGroup = this._fb.group({
name: [null, Validators.required],
surname: [null, Validators.required],
address: [null, Validators.required],
phone: [null, Validators.required],
});
constructor(private dashboardService: DashboardService, private userTypeService: UserTypeService, private _fb: FormBuilder, private userService: UserService) { }
ngOnInit() {
this.userTypeSubscription = this.userTypeService.userType.subscribe(userType => {
this.userType = userType;
if (this.userType == 'Doctor') {
this.GetDoctor();
}
if (this.userType == 'Patient') {
this.GetPatient();
}
})
}
GetDoctor() {
this.dashboardService.GetDoctorHome().subscribe(data => {
this.doctor = data;
},
error => {
console.log(error);
});
}
GetPatient() {
this.dashboardService.GetPatientHome().subscribe(data => {
this.patient = data;
},
error => {
console.log(error);
});
}
OpenPatientEdit() {
this.isEdit = !this.isEdit;
this.patientFormGroup.patchValue({
name: this.patient.name,
surname: this.patient.surname,
address: this.patient.address,
phone: this.patient.phone
});
// this.patientFormGroup.get('name').setValue(this.patient.name);
// this.patientFormGroup.get('surname').setValue(this.patient.surname);
// this.patientFormGroup.get('address').setValue(this.patient.address);
// this.patientFormGroup.get('phone').setValue(this.patient.phone);
}
EditPatient() {
this.OpenPatientEdit();
var patient: Patient = {
patientId: -1,
name: this.patientFormGroup.get('name').value,
surname: this.patientFormGroup.get('surname').value,
dob: null,
address: this.patientFormGroup.get('address').value,
phone: this.patientFormGroup.get('phone').value
}
debugger;
// this.userService.PutPatient(patient).subscribe((data) => {
// this.GetPatient()
// }, error => {
// console.log(error)
// })
}
}
Problem -问题-
I tried recreating the above on https://stackblitz.com/edit/angular-efnxdy?file=src%2Fapp%2Fapp.component.ts .我尝试在https://stackblitz.com/edit/angular-efnxdy?file=src%2Fapp%2Fapp.component.ts上重新创建上述内容。
The error is that you are calling this.OpenPatientEdit();
错误是您正在调用this.OpenPatientEdit();
, again in the EditPatient()
(I suggest renaming this method to saveEdit()
), which is again patching the values from the local patient field and the fields are updated back to the original changes. ,再次在EditPatient()
中(我建议将此方法重命名为saveEdit()
),这再次修补了本地患者字段中的值,并且这些字段被更新回原始更改。
Solution - Just change the OpenPatientEdit()
method to解决方案- 只需将OpenPatientEdit()
方法更改为
OpenPatientEdit() {
this.isEdit = true; <---------CHANGE THIS TO true, to show the form
this.patientFormGroup.patchValue({
name: this.patient.name,
surname: this.patient.surname,
address: this.patient.address,
phone: this.patient.phone
});
}
And `` as和``作为
EditPatient() {
this.isEdit = false; <---------CHANGE THIS TO false, to hide the form
var patient: Patient = {
patientId: -1,
name: this.patientFormGroup.get('name').value,
surname: this.patientFormGroup.get('surname').value,
dob: null,
address: this.patientFormGroup.get('address').value,
phone: this.patientFormGroup.get('phone').value
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.