Search This Blog

2023/09/02

How to load existing values of control after submit ?



Assume you have a form defined as follows

constructor(private fb: FormBuilder) {
this.searchForm = this.fb.group({
country: new FormControl('', Validators.required),
state: new FormControl('', Validators.required),
city: new FormControl('', []),
religion: new FormControl('', [Validators.required]),
caste: new FormControl('', []),
gender: new FormControl('', Validators.required)
});
}

on submit of this form you will call an api to accomplish certain tasks after that we need to load existing values of form control

lets assume that your api call also return params passed to it in api response under searchParams then you can load form control values

as follows

this.searchForm.patchValue(result.searchParams)


if you want to load value of only particular form control say birthtime then it can be done as


var selectedBirthTime = result.searchParams.birthtime
this.searchForm.patchValue({
'birthtime': selectedBirthTime
})

No comments:

Post a Comment