--- name: ng-data-access description: Build the typed server-communication layer in Angular v22+ – fetch data through httpResource/resource/rxResource backed by HttpClient, with functional interceptors, loading/error/reload state, and SSR-safe transfer caching. Use when fetching data, writing an API service, wiring HttpClient, choosing httpResource vs resource vs rxResource, adding an interceptor, or surfacing loading/error/reload state. The project's TypeScript style guide prefers resource()/rxResource()/httpResource() over "RxJS + async pipe" for reads. license: MIT metadata: author: Alexander Thalhammer version: '1.0' --- # Angular Data Access The server-communication layer is **signals-first**: a reactive request drives an async loader, and status/value/error come back as synchronous signals you read straight in an `OnPush` template. Reads go through `resource`-family primitives (preferred by `style-guide/style-guide.ts.md` over "RxJS + async pipe"); mutations stay imperative `HttpClient` calls. Everything routes through the `HttpClient` stack so interceptors, testing, and SSR transfer cache apply uniformly. For exhaustive resource API depth, defer to the canonical [`../angular-developer/references/resource.md`](../angular-developer/references/resource.md) (synced from Angular's official skill) when that skill is installed next to this one, otherwise to the resource guides on angular.dev. This skill owns the project-shaped layer: wiring, service shape, interceptors, error UX, and SSR. ## 1. Wire HttpClient first Check `app.config.ts` first: if it lacks **`provideHttpClient`**, add it before any fetch works. Use `withFetch()` so requests run on the Fetch API (SSR-friendly, streaming-capable): ```typescript // src/app/app.config.ts import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http'; export const appConfig: ApplicationConfig = { providers: [ // …existing providers, incl. provideClientHydration(withEventReplay()) provideHttpClient(withFetch(), withInterceptors([/* fns */])), ], }; ``` `provideClientHydration()` is already present, so the **HTTP transfer cache is on by default** – any HttpClient-backed fetch during SSR is replayed on the client without a second request. See [`references/interceptors-and-ssr.md`](references/interceptors-and-ssr.md). _Done when_ `provideHttpClient(withFetch())` is in `app.config.ts` and the app builds. ## 2. Pick the primitive | Need | Use | | ------------------------------------------------------------------------- | ------------------------------------------- | | Reactive read through HttpClient (interceptors, transfer cache) | **`httpResource`** – default for reads | | Async read from a non-HttpClient source (fetch, SDK, promise) | **`resource`** | | Read whose loader is naturally an Observable (debounce, retry, switchMap) | **`rxResource`** | | One-shot call or a mutation (POST/PUT/PATCH/DELETE on user action) | **plain `HttpClient`** (returns Observable) | `resource`/`httpResource`/`rxResource` are **read-only** – they cancel in-flight loads via `AbortSignal` on destroy or param change, which would abort a mutation. Never fetch a mutation through them. Details and verified examples: [`references/resource-apis.md`](references/resource-apis.md). ### Worked example – signal params → httpResource → template states ```typescript import { Component, signal } from '@angular/core'; import { httpResource } from '@angular/common/http'; import { User } from './user-api'; @Component({ selector: 'app-user-card', template: ` @if (user.isLoading()) {
Loading…
} @else if (user.error()) {Couldn't load user.
} @else {{{ user.value()?.name }}
} `, }) export class UserCard { protected readonly userId = signal('123'); // Re-fetches whenever userId() changes; undefined URL ⇒ resource stays idle. protected readonly user = httpResource