Sub topic 7
Handling outputs in the parent
This chapter covers the parent's side of an output: how a template listens to it, what $event holds, and how handlers behave in practice. It assumes you know TypeScript classes and generics, standalone components and template syntax, and the basics of property and event binding. You should also know that a signal is read by calling it. The previous chapter showed how a child declares an output with output() and sends a value with emit(). Here the focus moves to the component that receives the event.
Listening with event binding
A parent listens to a child's output with the same syntax it uses for native DOM events: the output name in parentheses, set equal to a statement.
The child from the previous chapter looks like this:
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 template binds to changed like this:
<app-counter [step]="5" (changed)="onChanged($event)" />Each time the child calls changed.emit(value), Angular runs the statement onChanged($event) in the parent. The name in parentheses must match the output's property name in the child (or its alias, covered later in the tutorial).
Angular treats an output binding as a subscription that lives as long as the child view. You do not unsubscribe by hand. When the child is destroyed, Angular removes the listener.
What `$event` contains
Inside the handler statement, $event is the value passed to emit(). Its type comes from the generic argument of output<T>().
| Child declaration | `emit()` call | Type of `$event` |
|---|---|---|
| `output<number>()` | `emit(3)` | `number` |
| `output<string>()` | `emit('a')` | `string` |
| `output<{ id: number }>()` | `emit({ id: 7 })` | `{ id: number }` |
| `output()` | `emit()` | `void` |
Because Angular's template type checker knows the generic argument, passing $event to a handler with an incompatible parameter type is a compile-time error. This is a large improvement over untyped event emitters.
Here is a parent that handles the counter's event:
import { Component } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-dashboard',
imports: [Counter],
template: `
<app-counter [step]="5" (changed)="onChanged($event)" />
<p>Last count: {{ lastCount }}</p>
`,
})
export class Dashboard {
lastCount = 0;
onChanged(count: number) {
this.lastCount = count;
}
}The parent must list Counter in its imports array. Without it, Angular does not recognize <app-counter> and reports an error.
Outputs without a payload
An output can carry no data. Declare it with no type argument and call emit() with no argument.
import { Component, output } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `<button (click)="closed.emit()">Close</button>`,
})
export class Dialog {
closed = output();
}The parent can then call a handler that takes no parameters:
<app-dialog (closed)="closeDialog()" />Do not use $event here. Its type is void, so it carries nothing useful.
Statements, not just method calls
The right-hand side of an event binding is a template statement. It can be more than a single method call. You can assign to a property, call several methods in sequence separated by semicolons, or use $event in an expression.
<app-counter (changed)="lastCount = $event" />
<app-counter (changed)="log($event); save($event)" />
<app-counter (changed)="onChanged($event * 2)" />Keep statements short. Anything beyond one call or one assignment is easier to read and test as a method on the class. Template statements cannot use new, increment operators such as ++, or chained assignments, which is another reason to move logic out of the template.
Handling signals in the parent
If the parent stores state in a signal, the handler writes to it with set() or update(). In the template you can also call these directly.
import { Component, signal } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-signal-dashboard',
imports: [Counter],
template: `
<app-counter (changed)="lastCount.set($event)" />
<p>Last count: {{ lastCount() }}</p>
`,
})
export class SignalDashboard {
lastCount = signal(0);
}lastCount.set($event) is a valid statement. Writing to a signal inside an event handler is safe, because event handlers are not reactive contexts.
The flow of one event
The sequence below shows what happens from the user's click to the parent's updated view.
The call to emit() is synchronous. By the time emit() returns, every listener has already run. This matters when the child does more work after emitting: the parent's handler has finished by then, and any state it changed is already updated.
Multiple listeners and multiple outputs
A component can have several outputs, and a parent can listen to any subset of them. Outputs you do not bind are simply ignored.
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-item-row',
template: `
<span>{{ label() }}</span>
<button (click)="selected.emit(id())">Select</button>
<button (click)="removed.emit(id())">Remove</button>
`,
})
export class ItemRow {
id = input.required<number>();
label = input('');
selected = output<number>();
removed = output<number>();
}<app-item-row [id]="item.id" [label]="item.name" (removed)="deleteItem($event)" />This parent handles removed and does nothing when selected fires.
The same output can also be bound more than once on one element. Angular runs every handler in the order the bindings appear. In practice this is rare, because a single handler that calls two methods is clearer.
Outputs in lists
Outputs are common inside @for loops, where each child reports which item it belongs to. Have the child include an identifier in the payload, as ItemRow does above. The parent then needs no extra bookkeeping.
import { Component, signal } from '@angular/core';
import { ItemRow } from './item-row';
interface Item {
id: number;
name: string;
}
@Component({
selector: 'app-item-list',
imports: [ItemRow],
template: `
@for (item of items(); track item.id) {
<app-item-row [id]="item.id" [label]="item.name" (removed)="deleteItem($event)" />
}
`,
})
export class ItemList {
items = signal<Item[]>([
{ id: 1, name: 'Apples' },
{ id: 2, name: 'Pears' },
]);
deleteItem(id: number) {
this.items.update((list) => list.filter((item) => item.id !== id));
}
}You could also write (removed)="deleteItem(item.id)" and let the child emit nothing. Both work. Putting the id in the payload keeps the child's contract self-contained, because the parent does not have to know which loop variable matters. Using the loop variable keeps the payload empty and avoids sending data the parent already has. Choose one style and apply it consistently.
Native events with the same name
A child output named like a native DOM event can cause confusion. If a component declares click = output<void>() and the parent writes (click)="..." on <app-x>, the handler is called for the output, and also for any native click bubbling from inside the child's host element. The parent then runs the handler twice for one click.
Handler errors
If a parent's handler throws, the exception propagates out of emit() into the child's code. The child's own method stops at the emit() line unless it catches the error. In most apps the exception ends up in Angular's ErrorHandler. Keep handlers free of code that can throw, or catch errors inside the handler, so a bug in a parent does not interrupt the child's logic.
Emitting at the right time
Two rules keep output behavior predictable.
- Emit from event handlers or reactive callbacks, not from the constructor. During construction, the parent's listeners are not yet connected, so an emit there is silently lost.
- Emit after the child's own state has been updated, as
Counter.increment()does. A parent that reads the child's state inside its handler then sees the new value.
Emitting after the component is destroyed raises an error, because the output is closed once the view is torn down. Cancel timers and subscriptions in the child before it is destroyed. Use DestroyRef or the takeUntilDestroyed() operator for that.
Summary of the parent's responsibilities
- Import the child component so its selector is recognized.
- Bind with
(outputName)="statement". - Use
$eventto receive the emitted value, typed by the child'soutput<T>(). - Keep the statement short and move real logic into a method.
- Do not unsubscribe manually. Angular manages the subscription.
Exercise
A parent template contains <app-item-row [id]="item.id" (removed)="deleteItem($event)" />, and ItemRow declares removed = output<number>(). What is the type of $event, and what happens if the parent does not bind (selected), another output of the same component?
Show answer
$event is a number, taken from the generic argument of output<number>(). Unbound outputs are ignored: when selected emits, no parent handler runs and nothing else happens.
Exercise
A component declares change = output<string>(). The parent writes <app-search (change)="onChange($event)" />, and onChange runs twice for one user edit. What is the likely cause, and how do you fix it?
Show answer
change is also a native DOM event name. The native event bubbles up from an inner <input> to the host element, so the handler runs once for the DOM event and once for the output. Rename the output to something distinct, such as valueChanged, so only the output triggers the handler.
Exercise
Write the parent template line that listens to closed = output() on <app-dialog> and calls closeDialog() without using $event.
Show answer
<app-dialog (closed)="closeDialog()" />