Sometimes a child component needs to send information back to its parent. This helps the parent know what the child did or wants.
Child to parent communication flow in Angular
In child component: @Output() eventName = new EventEmitter<type>(); To send data: this.eventName.emit(data); In parent template: <child-component (eventName)="parentHandler($event)"></child-component> In parent component: parentHandler(data: type) { // handle data }
The @Output decorator marks an event the child can send out.
The parent listens to this event using parentheses (eventName) in the template.
Child component: @Output() notify = new EventEmitter<string>(); sendMessage() { this.notify.emit('Hello from child'); } Parent template: <app-child (notify)="receiveMessage($event)"></app-child> Parent component: receiveMessage(msg: string) { console.log(msg); }
Child component: @Output() countChanged = new EventEmitter<number>(); increment() { this.count++; this.countChanged.emit(this.count); } Parent template: <counter (countChanged)="updateCount($event)"></counter> Parent component: updateCount(newCount: number) { this.currentCount = newCount; }
The child component has a button. When clicked, it sends a string to the parent using an event. The parent listens and shows the received data below the child.
/* child.component.ts */ import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-child', template: `<button (click)="sendData()">Send Data</button>` }) export class ChildComponent { @Output() dataSent = new EventEmitter<string>(); sendData() { this.dataSent.emit('Data from child'); } } /* parent.component.ts */ import { Component } from '@angular/core'; @Component({ selector: 'app-parent', template: ` <h2>Parent Component</h2> <app-child (dataSent)="handleData($event)"></app-child> <p>Received: {{ receivedData }}</p> ` }) export class ParentComponent { receivedData = ''; handleData(data: string) { this.receivedData = data; } }
Always import EventEmitter and @Output from @angular/core.
The parent uses $event to get the data sent by the child.
Use simple data types or objects for the event payload to keep communication clear.
Child to parent communication uses @Output and EventEmitter.
The child emits events, and the parent listens and reacts.
This keeps components loosely connected and easy to manage.
Practice
Solution
Step 1: Understand child to parent communication
Child components send data or events to parents using Angular's event system.Step 2: Identify Angular feature for this
@Outputdecorator withEventEmitterallows child to emit events the parent listens to.Final Answer:
Using @Output with EventEmitter -> Option DQuick Check:
Child to parent = @Output + EventEmitter [OK]
- Confusing @Input as child to parent communication
- Thinking services are needed for simple child to parent
- Using ViewChild to call parent methods directly
Solution
Step 1: Recall correct decorator and initialization
Event emitters must be decorated with@Output()and initialized withnew EventEmitter<type>().Step 2: Check each option
@Output() notify = new EventEmitter<string>(); correctly uses@Output()and initializes the emitter. Others miss decorator, use wrong decorator, or wrong syntax.Final Answer:
@Output() notify = new EventEmitter<string>(); -> Option CQuick Check:
@Output() + new EventEmitter() = correct syntax [OK]
- Using @Input instead of @Output
- Forgetting parentheses after @Output
- Not initializing EventEmitter with new keyword
@Output() change = new EventEmitter<number>();
updateValue() {
this.change.emit(42);
}What will the parent component receive when
updateValue() is called?Solution
Step 1: Understand what emit does
Theemitmethod sends the given value (here 42) as an event payload to the parent.Step 2: Check the emitted value type
The emitter is typed asnumberand emits 42, so the parent receives 42.Final Answer:
An event with value 42 -> Option AQuick Check:
emit(42) sends 42 to parent [OK]
- Assuming emit sends method name instead of value
- Thinking emit requires string only
- Believing no event is sent without explicit subscription
@Output notify = new EventEmitter<string>();
send() {
this.notify.emit('hello');
}Solution
Step 1: Check @Output decorator syntax
The@Outputdecorator requires parentheses:@Output().Step 2: Verify other parts
Type parameter on EventEmitter is allowed, emit can send strings, and notify can be public.Final Answer:
Missing parentheses after @Output decorator -> Option AQuick Check:
@Output() needs parentheses [OK]
- Omitting parentheses after @Output
- Thinking EventEmitter can't have types
- Assuming emit only accepts numbers
Child component:
@Output() sendData = new EventEmitter<{id:number, name:string}>();
onClick() {
const data = {id: 1, name: 'Test'};
this.sendData.emit(data);
}
Parent component template:
<app-child (sendData)="handleData($event)"></app-child>What should the parent component's
handleData method look like?Solution
Step 1: Understand emitted data type
The child emits an object withidandnameproperties.Step 2: Match parent handler parameter
The parent method must accept an object matching the emitted type to access properties likename.Final Answer:
handleData(data: {id: number; name: string}) { console.log(data.name); } -> Option BQuick Check:
Parent handler param matches emitted object type [OK]
- Ignoring event parameter in handler
- Using wrong parameter type in handler
- Not accessing object properties correctly
Ready to prove it?
Pass the test and this pattern moves unknown
