Angular @Output
Learn how to use @Output to send data from a child component to a parent component in Angular.
What is @Output?
In Angular, @Output is used when a child component needs to send information or notify an event to its parent component.
The child component creates a custom event using EventEmitter, while the parent component listens to that event using event binding.
This creates a simple and controlled way for components to communicate from Child → Parent.
Child to Parent Communication
Basic @Output Syntax
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-course-card',
templateUrl: './course-card.component.html'
})
export class CourseCardComponent {
@Output() courseSelected = new EventEmitter<string>();
}
selectCourse() {
this.courseSelected.emit('Java Full Stack');
}
<app-course-card
(courseSelected)="handleCourseSelection($event)">
</app-course-card>
How @Output Works
Create Output
The child component creates an event using @Output and EventEmitter.
Trigger Event
The child calls emit() when the required action occurs.
Parent Listens
The parent listens to the custom event using event binding.
Receive Data
The parent receives the emitted value through $event.
Simple @Output Example
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-course-card',
templateUrl: './course-card.component.html'
})
export class CourseCardComponent {
@Output() courseSelected =
new EventEmitter<string>();
selectCourse() {
this.courseSelected.emit('Python Full Stack');
}
}
<button
type="button"
(click)="selectCourse()">
Select Course
</button>
<app-course-card
(courseSelected)="receiveCourse($event)">
</app-course-card>
receiveCourse(course: string) {
console.log('Selected Course:', course);
}
Output
Python Full Stack
Course selected successfully at CIIT Training Institute.
Using Event Binding with @Output
Parent components use the (event) syntax to listen for a custom event generated by the child component.
<app-course-card
(courseSelected)="receiveCourse($event)">
</app-course-card>
Using EventEmitter
EventEmitter is used with @Output to create custom events that can be handled by the parent component.
The emit() method triggers the event and can send data along with the event.
@Output() enquiry =
new EventEmitter<string>();
sendEnquiry() {
this.enquiry.emit('Full Stack .NET');
}
Complete CIIT Training Institute Example
Suppose CIIT Training Institute has a reusable course card component. When a visitor clicks the Enquire Now button, the child component sends the selected course to the parent.
This communication is handled using @Output and EventEmitter.
import {
Component,
Output,
EventEmitter
} from '@angular/core';
@Component({
selector: 'app-course-card',
templateUrl: './course-card.component.html'
})
export class CourseCardComponent {
@Output() enquiry =
new EventEmitter<string>();
courseName = 'Full Stack .NET';
sendEnquiry() {
this.enquiry.emit(this.courseName);
}
}
<div class="course-card">
<h3>{{ courseName }}</h3>
<p>
Learn Full Stack Development
at CIIT Training Institute.
</p>
<button
type="button"
(click)="sendEnquiry()">
Enquire Now
</button>
</div>
<app-course-card
(enquiry)="receiveEnquiry($event)">
</app-course-card>
receiveEnquiry(course: string) {
console.log(
'Enquiry received for:',
course
);
}
Data Flow
Passing Different Types of Data
@Output() courseSelected =
new EventEmitter<string>();
this.courseSelected.emit('Java Full Stack');
@Output() feeChanged =
new EventEmitter<number>();
this.feeChanged.emit(35000);
@Output() courseDetails =
new EventEmitter<object>();
this.courseDetails.emit({
name: 'Python Full Stack',
duration: '6 Months',
mode: 'Online'
});
@Input vs @Output
| Feature | @Input | @Output |
|---|---|---|
| Direction | Parent → Child | Child → Parent |
| Purpose | Receive data | Send event/data |
| Main Feature | Input Property | EventEmitter |
| Binding | [property] | (event) |
| Example | [courseName] | (courseSelected) |
Important Points
@Output is mainly used for passing events or data from a child component to a parent component.
EventEmitter is used to create and emit custom events.
The emit() method triggers the event and sends the required value.
The parent component can access the emitted value using $event.
@Output Best Practices
- Use @Output for Child → Parent communication.
- Use meaningful custom event names.
- Use EventEmitter for custom events.
- Use emit() when the required action occurs.
- Send only the required data to the parent.
- Keep child components reusable and focused.
Summary
Angular @Output allows a child component to communicate with its parent component. The child creates a custom event using EventEmitter, sends data using emit(), and the parent listens to that event using event binding and receives the value through $event.