httpResource & resource()

Angular 20+ — Reactive async data fetching built on Signals

About This Feature

httpResource() and resource() replace the subscribe() pattern for data fetching. They expose request state as Signals — no manual subscriptions, no takeUntil, no loading booleans.

  • httpResource(requestFn) — wraps HttpClient. Pass a function returning a URL string or request config; it refetches automatically whenever the signals inside the function change. In-flight requests are cancelled (aborted) when the source signal changes.
  • resource({ request, loader }) — generic version. The loader is any async function (fetch, WebSocket, IndexedDB, etc.). Angular provides an abortSignal to cancel in-flight work.
  • Both expose: .value(), .status(), .isLoading(), .error(), .reload()

Code Example

import { httpResource } from '@angular/common/http';
import { resource, signal } from '@angular/core';

// ── httpResource() ────────────────────────────────────────
// Reactive HTTP — refetches automatically when signals change
selectedUserId = signal(1);

userResource = httpResource<User>(
  () => `/api/users/${this.selectedUserId()}`
);

// Reactive state — no subscribe(), no takeUntil(), no cleanup:
userResource.value()       // User | undefined
userResource.status()      // 'idle' | 'loading' | 'resolved' | 'error'
userResource.isLoading()   // boolean
userResource.error()       // unknown
userResource.reload()      // manually trigger a re-fetch

// ── resource() ───────────────────────────────────────────
// Generic async resource with any loader (fetch, DB, WebSocket...)
commentResource = resource<Comment[], number>({
  params: () => this.postId(),          // reactive signal ← renamed from "request"
  loader: ({ params: id, abortSignal }) =>
    fetch(`/api/comments?postId=${id}`, { signal: abortSignal })
      .then(r => r.json())              // abortSignal cancels on params change
});

// Same reactive API: .value(), .status(), .isLoading(), .reload()

Live Demo — httpResource()

Select a user ID — both user profile and posts refetch automatically (no subscribe):

userResource

Leanne Graham

Sincere@april.biz

1-770-736-8031 x56442

hildegard.org

status: resolved

postsResource (same userId signal)

sunt aut facere repellat provident occae…
qui est esse…
ea molestias quasi exercitationem repell…
eum et est occaecati…
nesciunt quas odio…

Live Demo — resource() with custom loader

resource() uses a raw fetch() call with automatic abort on signal change:

resolved
id labore ex et quam laborum — Eliseo@gardner.biz

laudantium enim quasi est quidem magnam voluptate ipsam eos tempora quo necessit…

quo vero reiciendis velit similique earum — Jayne_Kuhic@sydney.com

est natus enim nihil est dolore omnis voluptatem numquam et omnis occaecati quod…

odio adipisci rerum aut animi — Nikita@garfield.biz

quia molestiae reprehenderit quasi aspernatur aut expedita occaecati aliquam eve…

alias odio sit — Lew@alysha.tv

non et atque occaecati deserunt quas accusantium unde odit nobis qui voluptatem …