Sub topic 7
Handling outputs in the parent
This chapter assumes you can declare an output with output() and call emit(), and that you know Angular's event binding syntax (event)="...". It covers the parent's side: how to listen, what $event holds, and what the compiler checks for you.
Listening with event binding
A child component's output is listened to the same way as a DOM event. You put the output name in parentheses on the child's tag and give it a statement to run.
Here is the child from the previous chapter. It declares a typed output named changed.
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">+{{ step() }}</button>
<span>{{ count }}</span>
`,
})
export class Counter {
step = input(1);
changed = output<number>();
count = 0;
increment() {
this.count += this.step();
this.changed.emit(this.count);
}
}The parent imports Counter and listens to changed.
import { Component } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-dashboard',
imports: [Counter],
template: `
<app-counter [step]="5" (changed)="onChanged($event)" />
<p>Latest total: {{ total }}</p>
`,
})
export class Dashboard {
total = 0;
onChanged(value: number) {
this.total = value;
}
}Each click on the button calls emit() in the child. Angular then runs the parent's onChanged with the emitted value.
What $event contains
$event is a special name available only inside the handler statement. For an output, it is exactly the value passed to emit(). It is not a DOM event object. Because changed is declared as output<number>(), the template type checker knows $event is a number. If onChanged expected a string, compilation would fail.
You can also write a short statement inline instead of calling a method.
<app-counter (changed)="total = $event" />Use this only for trivial assignments. Anything longer is easier to read and test as a method.
Outputs without a payload
If an output carries no data, declare it as output<void>() and call emit() with no argument. The parent then ignores $event.
import { Component, output } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `<button (click)="closed.emit()">Close</button>`,
})
export class Dialog {
closed = output<void>();
}<app-dialog (closed)="isOpen = false" />Behavior to know
- Handlers run synchronously. The parent's handler finishes before
emit()returns in the child. - Several listeners are allowed. Each
(changed)binding on the same element is called. - Outputs do not bubble. A DOM event such as
clicktravels up through ancestors. An output reaches only the parent that binds it on the child's tag. A grandparent must be notified by a second output on the middle component. - Avoid native event names. An output called
clickorinputcan clash with the DOM event of the same name on the host element. Pick a specific name such aschangedorclosed. - Do not prefix names with
on. Name the outputchanged, notonChanged. The binding then reads(changed)="onChanged($event)", and the handler name stays distinct.
Exercise
A child declares selected = output<{ id: number; label: string }>(). In the parent template you write (selected)="current = $event.label". What type does $event have, and what happens if the child calls selected.emit() with no argument?
Show answer
$event has the type { id: number; label: string }. Calling emit() with no argument is a compile-time error, because the output's type is not void and requires a value.
Exercise
A page contains <app-list>, which contains <app-item>. app-item has an output removed. Can the page listen to (removed) on <app-list> directly? Why or why not?
Show answer
No. Outputs do not bubble. The page can only listen to removed on <app-item>'s own tag, so app-list must declare its own output and emit it when app-item emits.