Using @Output Decorator with EventEmitter in Angular
📖 Scenario: You are building a simple Angular app where a child component sends a message to its parent component when a button is clicked.
🎯 Goal: Create a child component that uses the @Output decorator with EventEmitter to send a message to the parent component. The parent component will listen and display the message.
📋 What You'll Learn
Create a child component with an @Output property named messageEvent that is an EventEmitter<string>.
Add a button in the child component template that triggers a method to emit a message string through messageEvent.
In the parent component template, listen to the messageEvent from the child component and bind it to a method named receiveMessage.
In the parent component class, create a message property to store the received message and display it in the template.
💡 Why This Matters
🌍 Real World
This pattern is common in Angular apps where child components need to communicate user actions or data changes to their parent components, such as form inputs, buttons, or custom controls.
💼 Career
Understanding @Output and EventEmitter is essential for Angular developers to build interactive and modular applications with clear component communication.
Progress0 / 4 steps
1
Create the child component with EventEmitter
In the child component TypeScript file, import Output and EventEmitter from '@angular/core'. Then create an @Output property called messageEvent that is a new EventEmitter<string>().
Angular
Hint
Remember to import Output and EventEmitter from '@angular/core'.
2
Add a button to emit a message
In the child component class, add a method called sendMessage that calls this.messageEvent.emit('Hello from Child'). In the child component template, add a button with a click event that calls sendMessage().
Angular
Hint
The button's click event should call the sendMessage method.
3
Listen to the event in the parent component
In the parent component template, add the child component selector <app-child> and bind its messageEvent to a method called receiveMessage($event).
Angular
Hint
Use the child component selector and bind messageEvent to receiveMessage($event).
4
Add message property and receiveMessage method in parent
In the parent component TypeScript file, create a message property initialized to an empty string. Add a method called receiveMessage that takes a message: string parameter and sets this.message to it.
Angular
Hint
Initialize message as an empty string and update it inside receiveMessage.
Practice
(1/5)
1. What is the main purpose of the @Output decorator in Angular?
easy
A. To allow a child component to send events to its parent component
B. To receive data from a parent component
C. To style the component's template
D. To import external modules into the component
Solution
Step 1: Understand the role of @Output
The @Output decorator is used in Angular to create a custom event that a child component can emit to notify its parent component.
Step 2: Differentiate from other decorators
Unlike @Input which receives data, @Output sends events upward. It does not style or import modules.
Final Answer:
To allow a child component to send events to its parent component -> Option A
Quick Check:
@Output sends events from child to parent [OK]
Hint: Remember: @Output sends events up from child to parent [OK]
Common Mistakes:
Confusing @Output with @Input
Thinking @Output styles components
Assuming @Output imports modules
2. Which of the following is the correct syntax to declare an @Output property named notify using EventEmitter in Angular?
easy
A. @Output() notify = new EventEmitter;
B. @Output notify = EventEmitter<string>();
C. @Output() notify = new EventEmitter<string>();
D. @Output() notify: EventEmitter<string> = EventEmitter();
Solution
Step 1: Recall correct EventEmitter syntax
The correct way is to use @Output() decorator with a property initialized as new EventEmitter<type>().
Step 2: Check each option
@Output() notify = new EventEmitter<string>(); correctly uses parentheses after @Output and calls new EventEmitter<string>(). Others miss parentheses or use wrong initialization.
Final Answer:
@Output() notify = new EventEmitter<string>(); -> Option C
Quick Check:
Use @Output() with new EventEmitter() [OK]
Hint: Always use parentheses: @Output() and new EventEmitter() [OK]
Common Mistakes:
Omitting parentheses after @Output
Not using 'new' keyword with EventEmitter
Incorrect property type declaration
3. Given this child component code:
import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Click` })
export class ChildComponent {
@Output() alertEvent = new EventEmitter<string>();
sendAlert() {
this.alertEvent.emit('Hello from child');
}
}
What happens when the button is clicked in the parent template listening as <child-comp (alertEvent)="showMessage($event)"></child-comp>?
medium
A. The child component reloads
B. Nothing happens because alertEvent is private
C. An error occurs because emit is not a function
D. The parent method showMessage is called with 'Hello from child' as argument
Solution
Step 1: Understand EventEmitter usage
The child component emits an event named alertEvent with the string 'Hello from child'.
Step 2: Parent listens to event
The parent template listens to alertEvent and calls showMessage passing the emitted string as $event.
Final Answer:
The parent method showMessage is called with 'Hello from child' as argument -> Option D
Quick Check:
Child emits event, parent handles it [OK]
Hint: Child emits event, parent listens with (eventName) [OK]
Common Mistakes:
Thinking emit is private or inaccessible
Assuming child reloads on emit
Confusing emit with method call error
4. Identify the error in this child component code:
import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Notify` })
export class ChildComponent {
@Output notify = new EventEmitter<string>();
notifyParent() {
this.notify.emit('Data sent');
}
}
medium
A. EventEmitter should not have a type parameter
B. Missing parentheses after @Output decorator
C. Method notifyParent should return a value
D. Template syntax for click event is incorrect
Solution
Step 1: Check @Output decorator syntax
The @Output decorator must have parentheses: @Output(). Missing parentheses causes a syntax error.
Step 2: Verify other parts
EventEmitter can have a type parameter, method does not need to return, and template click syntax is correct.
Final Answer:
Missing parentheses after @Output decorator -> Option B
Quick Check:
@Output() needs parentheses [OK]
Hint: Always write @Output() with parentheses [OK]
Common Mistakes:
Omitting parentheses after @Output
Thinking EventEmitter cannot have type
Expecting method to return value unnecessarily
5. You want a child component to emit an event only when a number input is positive. Which code correctly implements this using @Output and EventEmitter?
hard
A.
import { Component, Output, EventEmitter } from '@angular/core';
@Component({ selector: 'child-comp', template: `Send` })
export class ChildComponent {
@Output() positiveNumber = new EventEmitter<number>();
numberInput = 0;
checkAndEmit() {
if (this.numberInput > 0) {
this.positiveNumber.emit(this.numberInput);
}
}
}