The @Output decorator with EventEmitter lets a child component send messages or data to its parent component. It helps components talk to each other.
@Output decorator with EventEmitter in Angular
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="sendData()">Click me</button>` }) export class ChildComponent { @Output() eventName = new EventEmitter<string>(); sendData() { this.eventName.emit('data'); } }
The @Output decorator marks a property as an event the parent can listen to.
EventEmitter is used to create and send events with optional data.
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="notify()">Notify Parent</button>` }) export class ChildComponent { @Output() notifyParent = new EventEmitter<string>(); notify() { this.notifyParent.emit('Hello from child'); } }
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<input (input)="onInput($event.target.value)" />` }) export class ChildComponent { @Output() valueChanged = new EventEmitter<number>(); onInput(value: string) { this.valueChanged.emit(+value); } }
This example shows a child component sending a message to the parent when the button is clicked. The parent listens to messageEvent and updates its message display.
import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `<button (click)="sendMessage()">Send Message</button>` }) export class ChildComponent { @Output() messageEvent = new EventEmitter<string>(); sendMessage() { this.messageEvent.emit('Hi Parent!'); } } @Component({ selector: 'app-root', template: ` <h1>Parent Component</h1> <child-comp (messageEvent)="receiveMessage($event)"></child-comp> <p>Message from child: {{ message }}</p> ` }) export class AppComponent { message = ''; receiveMessage(msg: string) { this.message = msg; } }
Always import Output and EventEmitter from @angular/core.
The parent listens to the child's event using parentheses: (eventName)="handler($event)".
You can emit any data type, like strings, numbers, objects, or even no data.
@Output lets child components send events to parents.
EventEmitter creates and sends these events.
Parents listen to child events with (eventName) in the template.
Practice
@Output decorator in Angular?Solution
Step 1: Understand the role of @Output
The@Outputdecorator 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@Inputwhich receives data,@Outputsends events upward. It does not style or import modules.Final Answer:
To allow a child component to send events to its parent component -> Option AQuick Check:
@Output sends events from child to parent [OK]
- Confusing @Output with @Input
- Thinking @Output styles components
- Assuming @Output imports modules
@Output property named notify using EventEmitter in Angular?Solution
Step 1: Recall correct EventEmitter syntax
The correct way is to use@Output()decorator with a property initialized asnew EventEmitter<type>().Step 2: Check each option
@Output() notify = new EventEmitter<string>(); correctly uses parentheses after@Outputand callsnew EventEmitter<string>(). Others miss parentheses or use wrong initialization.Final Answer:
@Output() notify = new EventEmitter<string>(); -> Option CQuick Check:
Use @Output() with new EventEmitter() [OK]
- Omitting parentheses after @Output
- Not using 'new' keyword with EventEmitter
- Incorrect property type declaration
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>?Solution
Step 1: Understand EventEmitter usage
The child component emits an event namedalertEventwith the string 'Hello from child'.Step 2: Parent listens to event
The parent template listens toalertEventand callsshowMessagepassing the emitted string as$event.Final Answer:
The parent method showMessage is called with 'Hello from child' as argument -> Option DQuick Check:
Child emits event, parent handles it [OK]
- Thinking emit is private or inaccessible
- Assuming child reloads on emit
- Confusing emit with method call error
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');
}
}Solution
Step 1: Check @Output decorator syntax
The@Outputdecorator 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 BQuick Check:
@Output() needs parentheses [OK]
- Omitting parentheses after @Output
- Thinking EventEmitter cannot have type
- Expecting method to return value unnecessarily
@Output and EventEmitter?Solution
Step 1: Understand the condition for emitting
The event should emit only if the number input is positive, meaning greater than zero.Step 2: Check each option's condition
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); } } }emits only ifnumberInput > 0, which matches the requirement.import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `Send` }) export class ChildComponent { @Output() positiveNumber = new EventEmitter<number>(); numberInput = 0; emitNumber() { this.positiveNumber.emit(this.numberInput); } }emits unconditionally.import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `Send` }) export class ChildComponent { @Output() positiveNumber = new EventEmitter<number>(); numberInput = 0; send() { if (this.numberInput >= 0) { this.positiveNumber.emit(this.numberInput); } } }emits if number is zero or positive (>=0), which is incorrect.import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-comp', template: `Send` }) export class ChildComponent { @Output() positiveNumber = new EventEmitter<number>(); numberInput = 0; sendPositive() { if (this.numberInput < 0) { this.positiveNumber.emit(this.numberInput); } } }emits if number is negative (<0), which is wrong.Final Answer:
Option A correctly emits only when numberInput is positive -> Option AQuick Check:
Emit only if numberInput > 0 [OK]
- Emitting event without condition
- Using >= 0 instead of > 0
- Emitting on negative numbers
Ready to prove it?
Pass the test and this pattern moves unknown
