I'm working in forms and I have two components: my child component contains this formGroup Object :
employeeForm : FormGroup;
this.employeeForm = new FormGroup({
firstName:new FormControl(),
lastNAme:new FormControl(),
email:new FormControl()
});
the child Component conatins just a small part of the form and the parent componant, contains the global form with submit button..
my issue is: I want in the Parent Component when I click on submit button I get the form group from my child component and push it to my global form in my parent component.
I add output in my child component :
@Output() onFormGroupChange: EventEmitter<FormGroup> = new EventEmitter<FormGroup>();
but I don't know how to let the submit button in parent Comp take the FormGroup object from my child component and proceed to push data...
do you have any idea on how to achieve this?
Thanks in advance.
Best Regards.
The Angular documentation says "The @Output() decorator in a child component or directive lets data flow from the child to the parent." This is exactly what we want.
@Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.
To let Angular know that a property in a child component or directive can receive its value from its parent component we must use the @Input() decorator in the said child. The @Input() decorator allows data to be input into the child component from a parent component.
In this article we will learn how to pass data from parent component to the respective child component and vice versa in Angular 8. Pass data from parent to child component using @Input () decorator, which allows data to pass through templates and child to parent component using @Output () decorator with the help of Event Emitter.
Use the @Input() decorator in a child component or directive to let Angular know that a property in that component can receive its value from its parent component. It helps to remember that the data flow is from the perspective of the child component. So an @Input() allows data to be input into the child component from the parent component.
With ViewChild Approach it is not taking formgroup with component. In ViewChild approach you set public myFormGroup: FormGroup in your child component and initialize the myFormGroup in ngOnInit (), then it should be available trough ViewChild reference in your parent component.
The <parent-component> serves as the context for the <child-component>. @ Input () and @ Output () give a child component a way to communicate with its parent component. @ Input () allows a parent component to update data in the child component. Conversely, @ Output () allows the child to send data to a parent component.
You can pass the parent form to child component:
<form [formGroup]="parentForm">
<child-form [form]="parentForm"></child-form>
<button (click)="submit()">Submit</button>
</form>
In child-form.component.ts, you can use addControl:
@Input() form; // this is parentForm
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.form.addControl('firstName', new FormControl('', Validators.required));
}
When click submit, you'll be able to get the form data from child component too:
this.parentForm.value
Do something like this in child component:
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit{
@Output() private onFormGroupChange = new EventEmitter<any>();
employeeForm = new FormGroup({
firstName:new FormControl(),
lastNAme:new FormControl(),
email:new FormControl()
});
public ngOnInit() {
this.onFormGroupChange.emit(this.employeeForm);
}
}
And parent component: this.formCheck is actual formGroup we can use in html.
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent {
formCheck :any = ''
public onFormGroupChangeEvent(_event) {
this.formCheck = _event;
console.error(_event, this.formCheck['controls'])
}
}
Demo
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