
How do @Input and @Output work in Angular?
Natan Ferreira
- 0
- 41
In Angular, @Input and @Output are essential decorators that facilitate communication between components. They allow data to be passed from a parent to a child component (@Input) and events to be emitted from a child to a parent component (@Output).
@Input

It allows the child component to receive values from the parent component. The parent sets the value, and the child uses it.
The parent calls the child component, sending the value. The child component receives this value, and to do so, it is necessary to use @Input
.
@Output

It allows the child component to send data to the parent component.
It uses EventEmitter
to emit events that the parent can listen to. To do this, it is necessary to use @Output
in the child component to emit the event that the parent component listens to.
Hands on
Parent
<div id="parent">
<app-child [message]="'Hello from Parent!'" (notify)="onNotify($event)"></app-child>
<p>{{ receivedMessage }}</p>
</div>
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
standalone: false,
templateUrl: './parent.component.html',
styleUrl: './parent.component.css'
})
export class ParentComponent {
receivedMessage = '';
onNotify(message: string) {
this.receivedMessage = message;
}
}
Child
<div id="child">
{{ message }}
<button (click)="sendMessage()">Send Message to Parent</button>
</div>
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-child',
standalone: false,
templateUrl: './child.component.html',
styleUrl: './child.component.css'
})
export class ChildComponent {
@Input() message!: string;
@Output() notify = new EventEmitter<string>();
sendMessage() {
this.notify.emit("Hello from Child!")
}
}
This allows for efficient communication between Angular components!

Author
-
I am a seasoned Full Stack Software Developer with 8+ years of experience, including 6+ years specializing in Java with Spring and Quarkus. My core expertise lies in developing robust RESTful APIs integrated with Cosmos Db, MySQL, and cloud platforms like Azure and AWS. I have extensive experience designing and implementing microservices architectures, ensuring performance and reliability for high-traffic systems. In addition to backend development, I have experience with Angular to build user-friendly interfaces, leveraging my postgraduate degree in frontend web development to deliver seamless and responsive user experiences. My dedication to clean and secure code led me to present best practices to my company and clients, using tools like Sonar to ensure code quality and security. I am a critical thinker, problem solver, and team player, thriving in collaborative environments while tackling complex challenges. Beyond development, I share knowledge through my blog, NatanCode, where I write about Java, Spring, Quarkus, databases, and frontend development. My passion for learning and delivering innovative solutions drives me to excel in every project I undertake.