Sub topic 8
Emitting Events with output()
This chapter assumes you can build Angular components, bind to events in templates with (event)="...", and read signal inputs declared with input(). The RxJS section also assumes basic Observable knowledge.
Inputs carry data from a parent into a child. An output carries a notification in the other direction: the child tells the parent that something happened. The output() function declares such an event without decorators and without RxJS.
Declaring and emitting an output
Call output<T>() in a field initializer. T is the type of the value you emit. The call returns an OutputEmitterRef<T>, and you call its emit() method to fire the event.
import { Component, output, signal } from '@angular/core';
@Component({
selector: 'app-todo-form',
template: `
<input [value]="text()" (input)="text.set($any($event.target).value)" />
<button (click)="save()">Add</button>
<button (click)="cancelled.emit()">Cancel</button>
`,
})
export class TodoFormComponent {
text = signal('');
added = output<string>();
cancelled = output<void>();
save() {
const value = this.text().trim();
if (value) {
this.added.emit(value);
this.text.set('');
}
}
}For an event that carries no data, use output<void>() and call emit() with no arguments.
Listening in the parent
The parent binds to the output with the usual event syntax. $event holds the emitted value.
<app-todo-form (added)="addTodo($event)" (cancelled)="closeForm()" />Aliasing the public name
The alias option sets the name the parent binds to, while your class keeps its own field name:
changed = output<number>({ alias: 'valueChange' });Use aliases sparingly. They make it harder to find where an event is defined.
How output() differs from @Output()
| Aspect | `@Output() x = new EventEmitter<T>()` | `x = output<T>()` |
|---|---|---|
| Declaration | Decorator plus class instance | Plain function call |
| Based on RxJS | Yes, extends `Subject` | No |
| Type of the field | `EventEmitter<T>` | `OutputEmitterRef<T>` |
| Emit method | `emit()` | `emit()` |
| Cleanup | Manual subscription handling | Cleaned up when the component is destroyed |
An output is not a signal. It holds no current value, so you can't read it by calling added(). It is a stream of separate events, which is also why it pairs with model() instead of being replaced by it. model() keeps a value in sync. output() reports that something happened.
Bridging outputs and Observables
If an event source is already an Observable, outputFromObservable() from @angular/core/rxjs-interop exposes it as an output. Angular subscribes only while the component is alive.
import { Component } from '@angular/core';
import { outputFromObservable } from '@angular/core/rxjs-interop';
import { interval, map } from 'rxjs';
@Component({ selector: 'app-ticker', template: '' })
export class TickerComponent {
tick = outputFromObservable(interval(1000).pipe(map(n => n + 1)));
}Going the other way, outputToObservable(ref) turns any output into an Observable. This helps when you subscribe from code, for example in tests.
Common mistakes
- Calling the output like a signal.
this.added()is a type error. Usethis.added.emit(value). - Declaring outputs outside an injection context.
output()must run in a field initializer or the constructor, not inside a method or asetTimeout. - Expecting events to bubble. Component outputs are not DOM events. Only the direct parent's binding receives them, and a grandparent hears nothing unless the parent re-emits.
- Reusing native event names. If you name an output
click, a parent's(click)handler fires both for your event and for real DOM clicks on the host element. - Emitting during construction. No parent listener is attached yet, so the event is lost. Emit in response to user actions or later lifecycle work.
Exercise
A child declares selected = output<number>() and a teammate writes if (this.selected() > 0) { ... } to check the last selected id. Why does this fail, and what should they do instead?
Show answer
An OutputEmitterRef is not a signal and stores no value, so calling it is a type error. Keep the last value in a signal (for example lastSelected = signal(0)), set it right before calling selected.emit(id), and read lastSelected(). If the parent needs to keep the value in sync, use model() instead.
Exercise
A grandparent template has (added)="log($event)" on a component that wraps <app-todo-form>, but log never runs. What is wrong?
Show answer
Outputs don't bubble. The wrapper component must declare its own added = output<string>() and re-emit it: <app-todo-form (added)="added.emit($event)" />.