In this walkthrough, you will learn how to send/share the data from the child component to the parent component and get the answers to the following questions.
- What is @Output?
- What is an Event Emitter?
What is @Output?
@Output decorator is used to share the data from the child component to the parent component.
What is an event emitter?
An event emitter is used to create custom events for AngularComponent.
Mainly, the event emitter is used to pass data from a child component to a parent component using the @output decorator.
In short, sending data to the parent is publishing /emitting the vent, and the parent component listens the same.
Send Data from Child to Parent Component
Create an Angular Project called “AnguWalk” using the following CLI command.
Command
Example
![User]()
Move the cursor to inside the project folder and open Visual Studio code.
Command
Example
![Desktop]()
Note. Visual studio code will get started only if your system is configured with path and settings.
Now, we are going to Create a child component to issue the default city value.
Command
![Childdefacity]()
Open the childdefacity.component.ts file and update the following code.
Open the childdefacity.component.html file and update the following code.
Open the app.component.ts file and update the following code.
Open the app.component.ts file and update the following code.
Open the app.component.html file and update the following code.
OUTPUT
![Output]()
Default load and City value typed SURAT in the city text box.
After clicking on the SEND DEFAULT CITY button, the Shirdi value is set in the City Textbox.
Now, the SHIRDI value comes from the child component.
![Child component]()
You can check form control values in the console.
![Console]()
In this walkthrough, you have learned how to send data from the child component to the parent component.
Happy coding!