Sub topic 11
Using Observables with rxResource
This chapter assumes you know how to create a resource with params and a loader. It also assumes you know basic RxJS and HttpClient. If your data layer already returns Observables, rxResource lets you keep it.
What rxResource is
rxResource has the same model as resource. It reads signals in params, re-runs when they change, and exposes value, status, error, and isLoading as signals. The only difference is the loading function. Instead of an async loader that returns a Promise, you write a stream function that returns an Observable.
It lives in a separate entry point, @angular/core/rxjs-interop, so the core package does not depend on RxJS.
import { Component, inject, signal } from '@angular/core';
import { rxResource } from '@angular/core/rxjs-interop';
import { HttpClient } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user-rx',
template: `<p>{{ user.value()?.name }}</p>`,
})
export class UserRxComponent {
private readonly http = inject(HttpClient);
readonly userId = signal(1);
readonly user = rxResource({
params: () => ({ id: this.userId() }),
stream: ({ params }) => this.http.get<User>(`/api/users/${params.id}`),
});
}How it works
The resource subscribes to the Observable that stream returns and takes the emitted value as value(). When userId changes, the resource unsubscribes from the old Observable and calls stream again with the new params. Unsubscribing makes HttpClient cancel the in-flight request. You do not need to pass an abortSignal yourself, which is the main convenience over fetch.
Choosing between resource and rxResource
| Situation | Use |
|---|---|
| Data comes from `fetch` or any Promise | `resource` |
| Data comes from `HttpClient`, interceptors, or an existing service that returns Observables | `rxResource` |
| You need RxJS operators such as `map`, `retry`, or `switchMap` on the way in | `rxResource` |
Both give you the same status signals, so templates look the same for either.
Common errors
- Emitting more than one value. A resource holds one value at a time. Depending on your Angular version, the stream's emissions may each update
value(), or only the first may be used. Check the API reference for your version. If you want a single result, addtake(1)or use a request that completes, such asHttpClient. - Completing without emitting. An Observable that completes with no value leaves the resource in an error state in current releases, because there is nothing to put in
value(). Make sure the stream emits at least once. - Forgetting
provideHttpClient(). Ifinject(HttpClient)fails, addprovideHttpClient()to your application config. - Importing from the wrong place.
rxResourcecomes from@angular/core/rxjs-interop, not@angular/core. - Reading signals inside
stream. Only signals read inparamsare tracked. Signals read insidestreamdo not trigger a reload, so read them inparamsand use the value from theparamsargument.
Applying an operator
Because stream returns an Observable, you can shape the data before it reaches the resource.
import { Component, inject, signal } from '@angular/core';
import { rxResource } from '@angular/core/rxjs-interop';
import { HttpClient } from '@angular/common/http';
import { map, retry } from 'rxjs';
interface UserDto {
id: number;
full_name: string;
}
@Component({
selector: 'app-user-name',
template: `<p>{{ name.value() }}</p>`,
})
export class UserNameComponent {
private readonly http = inject(HttpClient);
readonly userId = signal(1);
readonly name = rxResource({
params: () => ({ id: this.userId() }),
stream: ({ params }) =>
this.http.get<UserDto>(`/api/users/${params.id}`).pipe(
retry(2),
map((dto) => dto.full_name),
),
});
}Here retry(2) makes up to two more attempts before the resource enters the error state, and map turns the response into the string the template needs.
Exercise
A component loads a user with HttpClient inside rxResource. The user changes the userId signal twice in quick succession. What happens to the first request, and why do you not need an abortSignal?
Show answer
The resource unsubscribes from the first Observable when userId changes, and HttpClient cancels the request on unsubscribe. The stream function returns an Observable, so cancellation comes from unsubscribing rather than from an abortSignal.
Exercise
Your stream function reads this.filter() inside the callback, not in params. You change filter, and nothing reloads. Why, and how do you fix it?
Show answer
Only signals read in params are tracked. Return the filter from params, for example params: () => ({ filter: this.filter() }), and use params.filter inside stream.