Below is my code to get a response from the service. Here I am getting a list of employees.
I need to bind form controls dynamically based on the response of service, my service returning more fields(EmployeeId, Name, Department etc.) than the form has controls. How to skip those which are not used in form control?
this._employeeService.getEmployeeById(this.employeeId).subscribe((res: Response) => {
this.employeeForm.get('FileUploader').setValue(null);
for (const field in res) {
this.employeeForm.controls[field].setValue(res[field]);
}
});
this.employeeForm = this._fb.group({
EmployeeId: 0,
Name: ''
});
To fetch the value of a form control, we have to use value property on the instance of FormControl in our class. In the same way we can fetch the value in HTML template.
boolean: the boolean value to check whether a form is touched or not.
While there already is an accepted answer, it's worth mentioning that there is indeed a method available just in case it might be useful to those actually wanting a concrete way to verify the existence of a given FormControl within a FormGroup:
contains(controlName: string): boolean
Source: https://angular.io/api/forms/FormGroup#contains
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With