Sub topic 12
Outputs and RxJS
This chapter assumes you can declare outputs with output(), handle them in a parent template, and read signals by calling them. You should also know the basics of RxJS: an Observable produces values over time, and a Subject is an observable you can push values into.
Why connect outputs to RxJS
An output() is the event channel from a child to its parent. Many Angular apps already use RxJS for event streams such as typing, debouncing, and HTTP calls. Angular ships two helpers in @angular/core/rxjs-interop that bridge the two worlds:
outputFromObservable()turns an observable into a component output.outputToObservable()turns an output into an observable.
Exposing an observable as an output
Say a search box should tell its parent what the user typed. A Subject receives each value, and outputFromObservable() publishes it as an output.
import { Component } from '@angular/core';
import { outputFromObservable } from '@angular/core/rxjs-interop';
import { Subject } from 'rxjs';
@Component({
selector: 'app-search-box',
template: `<input (input)="onInput($event)" />`,
})
export class SearchBox {
private query$ = new Subject<string>();
queryChanged = outputFromObservable(this.query$);
onInput(event: Event) {
this.query$.next((event.target as HTMLInputElement).value);
}
}Each call to query$.next() reaches the parent as an event. The parent listens the same way it listens to any output:
<app-search-box (queryChanged)="search($event)" />The parent cannot tell whether the output came from output() or from an observable. That keeps the child's internals private.
Shaping the stream first
The benefit of the observable route is that you can apply RxJS operators before the value leaves the component. This version waits for the user to pause and skips repeated values:
import { Component } from '@angular/core';
import { outputFromObservable } from '@angular/core/rxjs-interop';
import { Subject, debounceTime, distinctUntilChanged } from 'rxjs';
@Component({
selector: 'app-search-box',
template: `<input (input)="onInput($event)" />`,
})
export class SearchBox {
private query$ = new Subject<string>();
queryChanged = outputFromObservable(
this.query$.pipe(debounceTime(300), distinctUntilChanged())
);
onInput(event: Event) {
this.query$.next((event.target as HTMLInputElement).value);
}
}The parent now receives at most one event per 300 ms pause, and never the same string twice in a row.
Reading an output as an observable
outputToObservable() goes the other direction. You pass it an output, such as one from a child component, and get an observable. This is useful when you want to combine child events with other streams using operators like switchMap or merge.
import { Component, output } from '@angular/core';
import { outputToObservable } from '@angular/core/rxjs-interop';
import { map } from 'rxjs';
@Component({
selector: 'app-save-bar',
template: `<button (click)="saved.emit('draft')">Save</button>`,
})
export class SaveBar {
saved = output<string>();
savedUpper$ = outputToObservable(this.saved).pipe(
map((value) => value.toUpperCase())
);
}savedUpper$ emits 'DRAFT' each time the button is clicked.
How the two directions compare
| Function | Input | Result | Typical use |
|---|---|---|---|
| `outputFromObservable()` | An `Observable<T>` | An output that emits `T` | Publish a processed stream as an event |
| `outputToObservable()` | An output of `T` | An `Observable<T>` | Combine output events with RxJS operators |
Gotchas
- If the source observable errors, the output stops forwarding events. Handle errors with
catchErrorbefore you pass the stream in. - Outputs from
outputFromObservable()follow the observable's completion. After aSubjectcompletes, no further events reach the parent. - Use plain
output()when you have no stream logic. The interop functions add value only when RxJS operators are involved.
Exercise
A child component keeps a private Subject<string> called term$. You want the parent to receive the term only after the user stops typing for 400 ms. Which function do you use, and where do you apply debounceTime(400)?
Show answer
Use outputFromObservable(). Apply the operator on the observable you pass in, for example outputFromObservable(this.term$.pipe(debounceTime(400))), in a field initializer. The parent listens with (termChanged)="..." like any other output.
Exercise
Why can't you call emit() on an output created with outputFromObservable()?
Show answer
That output is a read-only forwarding channel for the source observable. You send values by calling next() on the source, such as a Subject.