Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Send FormGroup Object as Output to Parent Component from child component n Angular

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.

like image 518
James Avatar asked Jun 30 '19 15:06

James


People also ask

Can we pass data from child to parent in Angular?

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.

How does a child component communicate with a parent component in Angular?

@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.

What is one way you can pass data from a parent component to a child 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.

How to pass data from parent component to child component in angular?

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.

How to use @input () in angular?

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.

How to take formgroup from parent to child 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.

What is the difference between the <parent-component> and <child-component> components?

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.


2 Answers

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
like image 124
katwhocodes Avatar answered Oct 12 '22 07:10

katwhocodes


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

like image 45
Anil Arya Avatar answered Oct 12 '22 07:10

Anil Arya