← Back to Angular, page 3

angular-0049 · Hard senior

What is the resource() API, how does it relate to signals and RxJS, and what is its status in Angular 20?

resource() loads asynchronous data into signals. It re-runs its loader whenever the signals read in its params function change, cancels the previous load with an AbortSignal, and exposes value, status, error, isLoading and reload as signals. rxResource does the same with Observables. In Angular 19 and 20 these APIs are experimental, so the API may still change.

Full answer

Before resource, loading data on a signal change meant toObservable, switchMap, and toSignal, plus manual loading and error flags. A resource packages that pattern.

ts
course = resource({
  params: () => ({ id: this.id() }),
  loader: async ({ params, abortSignal }) => { ... fetch(..., { signal: abortSignal }) ... },
});

How it works:

  • params is a reactive function. When the signals it reads change, the loader runs again. If params returns undefined, the resource goes to idle and does not load, which handles "no id yet".
  • loader returns a Promise. It receives the current params and an abortSignal. When params change before the previous load finishes, the old load is aborted and its result is ignored.
  • State signals: value(), status() (idle, loading, reloading, resolved, error, local), error(), and isLoading(). hasValue() is a safe check. reload() runs the loader again. set() and update() write a local value, for example for optimistic updates.
  • rxResource (in @angular/core/rxjs-interop) uses a stream function that returns an Observable, so HttpClient, interceptors, and testing tools work unchanged.

Status warning: resource, rxResource, and httpResource are marked experimental in Angular 19 and 20 (the parameter name changed from request to params in 20). Do not build a core library on them without a plan for changes.

When to use: simple "fetch when this signal changes" cases. When not: complex flows with debounce, retries, merging several streams, or write operations (use RxJS), and mutations (resource is for reading).

Remember to handle the error state in the template. Reading value() while in error throws, so check hasValue() or status() first.

typescript
import { HttpClient } from '@angular/common/http';
import { Component, inject, input, resource } from '@angular/core';
import { rxResource } from '@angular/core/rxjs-interop';

interface Course {
  id: string;
  title: string;
}

@Component({
  selector: 'app-course-view',
  template: `
    @if (course.isLoading()) {
      <p>Loading...</p>
    } @else if (course.error(); as err) {
      <p role="alert">Could not load: {{ err.message }}</p>
      <button (click)="course.reload()">Try again</button>
    } @else if (course.hasValue()) {
      <h1>{{ course.value().title }}</h1>
    } @else {
      <p>Choose a course.</p>
    }
  `,
})
export class CourseViewComponent {
  private readonly http = inject(HttpClient);
  readonly id = input<string>();

  // Promise based: fetch plus AbortSignal.
  readonly course = resource({
    params: () => this.id(), // undefined means idle
    loader: async ({ params, abortSignal }): Promise<Course> => {
      const response = await fetch(`/api/courses/${params}`, { signal: abortSignal });
      if (!response.ok) {
        throw new Error(`Server answered ${response.status}`);
      }
      return (await response.json()) as Course;
    },
  });

  // Observable based: same idea with HttpClient and its interceptors.
  readonly related = rxResource({
    params: () => this.id(),
    stream: ({ params }) => this.http.get<Course[]>(`/api/courses/${params}/related`),
    defaultValue: [] as Course[],
  });
}

Angular 20.3 (experimental API, may change). Needs `provideHttpClient()` for the `rxResource` example and an API that returns JSON for both.

Key points

  • `params` is reactive; the loader reruns when it changes. `undefined` params means idle.
  • Old loads are aborted through `abortSignal`.
  • `value`, `status`, `error`, `isLoading`, `reload`, `set` are all part of the resource.
  • Experimental in Angular 19 and 20. Use RxJS for complex flows.

Common mistakes

  • Reading `value()` in the error state without a check, which throws.
  • Ignoring `abortSignal`, so cancelled requests still finish and use bandwidth.
  • Using resource for writes such as POST requests.

Follow-ups

  • How does `rxResource` differ from `resource`?
  • What is the difference between `loading` and `reloading`?
  • When would you still choose `toObservable` and `switchMap`?

Tips

  • State the experimental status first. Interviewers value honesty about maturity.
  • Compare it with the signal plus `switchMap` pattern to show you understand what it replaces.
  • Mention `abortSignal` and the idle state with undefined params.

Evaluation rubric

  • Explains reactive params, loader, and abort.
  • Lists the resource state signals.
  • States the experimental status and limits.