Explain it simply
@Output() - Child to Parent, in plain language
@Output() and EventEmitter are used to pass events/data from a child component up to a parent component.
Problem → Why → Solution
The problem — why this concept matters
Emitting an entire state object as every output payload tightly couples components.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
An output is a typed event contract that sends child intent or results to a parent without directly modifying parent data. Keep payloads minimal and domain-specific, and avoid names that collide with native DOM events. Broader sibling or application coordination usually belongs in a service or state layer rather than an output-based command bus.
- user input
- binding and validation
- valid state or feedback
Explain the core concept and Angular data flow of @Output() - Child to Parent
Core syntax or pattern
// child.component.ts
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<button (click)="sendData()">Send to Parent</button>
`
})
export class ChildComponent {
@Output() dataEvent = new EventEmitter<string>();
sendData() {
this.dataEvent.emit('Hello from Child!');
}
}
// parent.component.ts
@Component({
selector: 'app-parent',
template: `
<app-child (dataEvent)="receiveData($event)"></app-child>
<p>{{ message }}</p>
`
})
export class ParentComponent {
message = '';
receiveData(data: string) {
this.message = data;
}
}An output is a typed event contract that sends child intent or results to a parent without directly modifying parent data. Keep payloads minimal and domain-specific, and avoid names that collide with native DOM events. Broader sibling or application coordination usually belongs in a service or state layer rather than an output-based command bus.
Read the code step by step
- user input
// child.component.ts@Output() and EventEmitter are used to pass events/data from a child component up to a parent component.
- binding and validation
// parent.component.tsAn output is a typed event contract that sends child intent or results to a parent without directly modifying parent data. Keep payloads minimal and domain-specific, and avoid names that collide with native DOM events. Broader sibling or application coordination usually belongs in a service or state layer rather than an output-based command bus.
- valid state or feedback
}Have a task row emit toggle and delete intent while its parent performs immutable list updates, sending only the required ID.
- user input
- binding and validation
- valid state or feedback
Success
Explain the core concept and Angular data flow of @Output() - Child to Parent
Failure
Emitting an entire state object as every output payload tightly couples components.
@Output() and EventEmitter are used to pass events/data from a child component up to a parent component.
// child.component.ts
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<button (click)="sendData()">Send to Parent</button>
`
})
export class ChildComponent {
@Output() dataEvent = new EventEmitter<string>();
sendData() {
this.dataEvent.emit('Hello from Child!');
}
}
// parent.component.ts
@Component({
selector: 'app-parent',
template: `
<app-child (dataEvent)="receiveData($event)"></app-child>
<p>{{ message }}</p>
`
})
export class ParentComponent {
message = '';
receiveData(data: string) {
this.message = data;
}
}(Clicking the button in the child component makes 'Hello from Child!' appear in the parent component)Real-world usage
How to apply it in a real project
Have a task row emit toggle and delete intent while its parent performs immutable list updates, sending only the required ID.
Mini challenge
Build it yourself
Rebuild the @Output() - Child to Parent example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// child.component.ts
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<button (click)="sendData()">Send to Parent</button>
`
})
export class ChildComponent {
@Output() dataEvent = new EventEmitter<string>();
sendData() {
this.dataEvent.emit('Hello from Child!');
}
}
// parent.component.ts
@Component({
selector: 'app-parent',
template: `
<app-child (dataEvent)="receiveData($event)"></app-child>
<p>{{ message }}</p>
`
})
export class ParentComponent {
message = '';
receiveData(data: string) {
this.message = data;
}
}Expected: Explain the core concept and Angular data flow of @Output() - Child to Parent
Show hint
Have a task row emit toggle and delete intent while its parent performs immutable list updates, sending only the required ID.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- user input → binding and validation → valid state or feedback
- An output is a typed event contract that sends child intent or results to a parent without directly modifying parent data. Keep payloads minimal and domain-specific, and avoid names that collide with native DOM events. Broader sibling or application coordination usually belongs in a service or state layer rather than an output-based command bus.
- Emitting an entire state object as every output payload tightly couples components.
- Have a task row emit toggle and delete intent while its parent performs immutable list updates, sending only the required ID.
Angular — Components — Angular