I have a form group that is being passed into the component as input parameter and I need to pass that to a method as an object of the class. Is there a easy way to convert the form group which contains the form control into a class
Class
JavaScript
x
6
1
export class CustomerClass{
2
custNo?: string;
3
CustName?: string;
4
CustAddress: string;
5
}
6
FormGroup
JavaScript
1
6
1
const group: FormGroup = this._fb.group({
2
CustAddress: this._fb.control('test'),
3
custNo: this._fb.control(''),
4
CustAddress: this._fb.control(''),
5
});
6
Component
JavaScript
1
11
11
1
@Input()
2
CustomerGroup: FormGroup;
3
4
//Now I need this as a object of class to pass it to a function
5
6
submit() {
7
this.customerservice.processCustomer(CustomerGroup);
8
9
}
10
11
Service
JavaScript
1
5
1
processCustomer(customer: CustomerClass){
2
//do stuff
3
}
4
5
Can I easily convert the form group to a class? How can I do that?
Advertisement
Answer
Did you try by getting the value from the form ? It will returns an object with the class properties.
JavaScript
1
6
1
this.customerservice.processCustomer(this.myForm.value);
2
3
//in your case if you want to send the form object that you are getting in the input.
4
5
this.customerservice.processCustomer(this.CustomerGroup?.value);
6