Full answer
Signals are good for current state. RxJS is good for events over time: waiting, cancelling, retrying. A real app uses both, and @angular/core/rxjs-interop joins them.
toSignal(obs$, { initialValue }) subscribes at once, stores the latest value, and unsubscribes when the owner is destroyed. Without an initialValue the type is T | undefined until the first emission. Use requireSync: true only for Observables that emit synchronously, such as BehaviorSubject. If the Observable errors, reading the signal throws the error, so handle errors in the stream first.
toObservable(sig) emits the signal's value through an effect, so it is delivered asynchronously and consecutive synchronous changes are merged into one emission.
Both need an injection context, or an injector option.
In the search example, the query is a signal bound to the input. It becomes an Observable, debounceTime(300) waits until the user stops typing, distinctUntilChanged() skips repeated values, and switchMap cancels the older HTTP request when a new one starts. The result becomes a signal for the template. Because HTTP is a one-shot Observable that completes, switchMap is the right flattening operator here.
Angular's resource and httpResource APIs (experimental in Angular 20) aim to cover simple loading cases, but RxJS stays the right choice for debouncing and complex flows.
import { HttpClient } from '@angular/common/http';
import { Component, inject, signal } from '@angular/core';
import { toObservable, toSignal } from '@angular/core/rxjs-interop';
import { catchError, debounceTime, distinctUntilChanged, of, switchMap } from 'rxjs';
@Component({
selector: 'app-course-search',
template: `
<input #box placeholder="Search courses" (input)="query.set(box.value)" />
<ul>
@for (hit of results(); track hit) {
<li>{{ hit }}</li>
} @empty {
<li>No results</li>
}
</ul>
`,
})
export class CourseSearchComponent {
private readonly http = inject(HttpClient);
readonly query = signal('');
readonly results = toSignal(
toObservable(this.query).pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap((q) =>
q.length < 2
? of<string[]>([])
: this.http
.get<string[]>('/api/search', { params: { q } })
.pipe(catchError(() => of<string[]>([]))),
),
),
{ initialValue: [] as string[] },
);
}Angular 20.3 and RxJS 7. Needs `provideHttpClient()` in the app config and an API that answers `GET /api/search?q=...` with a JSON array of strings.
Key points
- `toSignal` subscribes immediately, cleans up automatically, and needs an `initialValue` (or `requireSync`).
- `toObservable` emits asynchronously and merges synchronous changes.
- Both need an injection context.
- Use `switchMap` to cancel outdated requests, and handle errors inside the inner stream.
Common mistakes
- Calling `toSignal` inside a method or event handler, outside an injection context.
- Letting an HTTP error reach `toSignal`, which then throws on every read.
- Putting `catchError` on the outer stream so one failure ends the whole search.
Follow-ups
- Why `switchMap` and not `mergeMap` for typeahead?
- What does `toObservable` emit when you set a signal twice in a row?
- When would you use `resource` instead of this pattern?
Tips
- Draw the pipeline: signal, observable, operators, signal. It is easy to follow.
- Say where the error handling goes (inside `switchMap`). This is what interviewers check.
- Show `initialValue` and say what the type becomes without it.
Evaluation rubric
- Uses toSignal and toObservable correctly.
- Chooses debounce, distinctUntilChanged, and switchMap with reasons.
- Handles errors and injection context.