Last updated Feb 20, 2021

How to Use @Output Decorator in Angular 14+

Master event communication using @Output in Angular.

How to Use @Output Decorator in Angular 14+

In the last Tutorial, we learned about how to pass data from the Parent to Child Component, Here we will learn How to pass data from the Child to Parent Component with the help of an Output decorator.

so, let’s get started…

What is an Output decorator?

Output decorator binds a property of a component to send data from child component to parent component.
-

In this scenario, the child component does not have any reference to the parent component.

So in this case, we need to emit an event from the child component by using eventEmitter, and then the parent component will listen to this event and receive the data.

Also Read, How to use @input Decorator in Angular

Prerequisite

In order to create this sample app, you must have Node JS and Angular development environments set up on your machine.

For this Please follow this link, How To Install Angular 14 – Tutorial

Step by Step Guide for Passing Data from Child to Parent Component in Angular:

Step 1: Create New Project

Create Blank Project in angular using the below command, Skip this step if you have already:

ng new angularDemo

Step 2: Add Components

Now add two new components with name parent and child component, using below command

ng g c parent 
ng g c child

Step 3: Add parent Component

To render our parent component HTML we need to include it to app.component.html file as HTML tag as shown below:

app.component.html:

Welcome to {{ name }}


Step 4: HTML for Parent and Child Component

Now suppose we have a scenario where we have to change the price from parent component and it should be effect on child component, so let’s do HTML for this as below:

HTML for parent component:

Parent component

Current Price : {{price}}

 HTML for child component:

Child component

In Parent HTML code we have added <app-child> tag to use it as child component.

 Step 5: Pass data using Output decorator and Eventemitter

 Now, add an event listener to child component input as follows:

Child component

Next, to make able parent component to receive value from child component we need to use output decorator with eventEmitter which allows us to emit the event and then parent component will listen to this event, see below code:

import { Component, OnInit,EventEmitter,Output } from '@angular/core';
@Component({selector: 'app-child',  
templateUrl: './child.component.html', 
styleUrls: ['./child.component.css']
export class ChildComponent implements OnInit 
constructor() { }
@Output() public newPrice = new EventEmitter();
 public price:any; ngOnInit() { }  onChange(value) {   this.newPrice.emit(value); }}

Step 6: Listen to Event from Child Component

Then, we need to listen emit event from child event emitter, so update HTML and Ts dile as below in as below:

HTML:

Parent component

Current Price : {{price}}

TS:

import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css'],
})
export class ParentComponent {
  public price: any;
  onchange(value) {
    this.price = value;
  }
}

Step 7: Run Application

Now our Application is ready, run application using ng serve command then go to a browser it will look like this: 

Now you can update any value from the child component input and it will emit to the parent component then it will display the received value.

So, using the Output decorator we can pass data from the Child component to the parent component.

Conclusion

In this article, we have seen, how we can use an output decorator to pass data from parent to child components in an easy way.

Happy Coding...🙂

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.