Angular EventEmitter
Learn how EventEmitter works with @Output to send events and data from a child component to a parent component in Angular.
What is EventEmitter?
In Angular, EventEmitter is used with @Output to create custom events in a child component.
The child component can use the emit() method to send information to its parent component.
This provides a clear communication flow from Child → Parent.
EventEmitter Communication Flow
Basic EventEmitter Syntax
import { Component, Output, EventEmitter } from '@angular/core';
@Output() courseSelected =
new EventEmitter<string>();
this.courseSelected.emit('Java Full Stack');
How EventEmitter Works
Create Event
Create a custom event using @Output and EventEmitter.
User Action
A button click or another action triggers the event.
Emit Data
The emit() method sends the event and optional data.
Parent Receives
The parent receives the emitted value using $event.
Simple EventEmitter 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 emit()
The emit() method is used to trigger an EventEmitter event.
It can send a value from the child component to the parent component.
@Output() enquiry =
new EventEmitter<void>();
sendEnquiry() {
this.enquiry.emit();
}
@Output() courseSelected =
new EventEmitter<string>();
selectCourse() {
this.courseSelected.emit('Full Stack .NET');
}
EventEmitter with Event Binding
The parent component listens to the custom event using Angular event binding.
<app-course-card
(courseSelected)="receiveCourse($event)">
</app-course-card>
EventEmitter with Different Data Types
@Output() course =
new EventEmitter<string>();
this.course.emit('Java Full Stack');
@Output() fees =
new EventEmitter<number>();
this.fees.emit(35000);
@Output() availability =
new EventEmitter<boolean>();
this.availability.emit(true);
@Output() courseDetails =
new EventEmitter<object>();
this.courseDetails.emit({
name: 'Python Full Stack',
duration: '6 Months',
mode: 'Online'
});
Complete CIIT Training Institute Example
Suppose CIIT Training Institute has a reusable course card. When a visitor clicks the Enquire Now button, the child component sends the selected course information to the parent component.
EventEmitter handles this Child → Parent communication.
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
);
}
Event Flow
Important Points
EventEmitter is generally used with @Output to create custom events.
The emit() method triggers the custom event.
Data can be passed as an argument to emit().
The parent accesses emitted data through $event.
EventEmitter Best Practices
- Use meaningful custom event names.
- Use EventEmitter with @Output for Child → Parent communication.
- Use emit() only when the required event occurs.
- Use appropriate TypeScript types for emitted values.
- Send only the required information to the parent.
- Keep communication between components simple and clear.
Summary
EventEmitter is used with Angular @Output to create custom events. The child component uses emit() to send an event or data, while the parent listens to the event using event binding and receives the value through $event.