I'm patching values to field in reactive form using the below code :
editPat(patient: Patient) {
this.regForm.patchValue({
_id: patient._id,
firstName: patient.firstName,
lastName: patient.lastName,
disease: patient.disease,
department: patient.department,
doctor: patient.doctor
});
}
In this function department
and doctor
formcontrols are containing select field and value does not appear in select . The value i'm assigning to formcontrol here that is value of option..
problem how can i assign value to select through patchValue
.Please help me
<div class="form-group ">
<label for="department">Department</label>
<select class="form-control" formControlName="department">
<option *ngFor="let department of departments"
value="{{ department._id }}">{{ department.Name }}</option>
</select>
</div>
Actually i'm using this code and after saving data in db (ie id) when i tried to patch value (for update option) other fields works but for select its does not work..
You can either try the another way to patch the value ,
UPDATED CODE
HTML file
<div class="form-group ">
<label for="department">Department</label>
<select (change)="patchValue($event.target.value)" class="form-control" formControlName="department">
<option *ngFor="let department of departments"
value="{{ department._id }}">{{ department.Name }}</option>
</select>
</div>
.ts file
patchValue(value){
this.regForm.patchValue({
department : value
});
this.regForm.get('department').updateValueAndValidity();
console.log(this.regForm.value);
}
Checkout my updated code .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.