Sub topic 3
Your first httpResource
This chapter assumes you know TypeScript, Angular components and templates, and signals (signal, computed, and reading signals in templates). It also assumes basic HTTP knowledge: methods, query parameters, and status codes. httpResource needs Angular 19.2 or later, and it is marked experimental in some releases, so check the release notes for your version.
What you are building
An httpResource is an object that fetches data with a GET request and exposes the outcome as signals. You declare it once, as a field on a component. Angular sends the request and keeps the signals up to date as the request progresses.
Step 1: provide HttpClient
httpResource uses HttpClient internally, so the application must provide it. Add provideHttpClient() to the application config:
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient()],
};Without this provider, the resource fails with a missing-provider error.
Step 2: declare the resource
Call httpResource with a function that returns a URL. The type argument describes the shape of the JSON body you expect.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user',
template: `
<button (click)="next()">Next user</button>
@if (user.isLoading()) {
<p>Loading…</p>
}
@if (user.error()) {
<p>Could not load the user.</p>
}
<p>{{ user.value()?.name }}</p>
`,
})
export class UserComponent {
userId = signal(1);
user = httpResource<User>(() => `/api/users/${this.userId()}`);
next() {
this.userId.update((id) => id + 1);
}
}The argument to httpResource is a request function. Angular calls it inside a reactive context. Any signal it reads becomes a dependency. Here it reads userId, so each call to next() triggers a new GET request.
What the resource exposes
The returned object has signals you can read in the template:
| Signal | Meaning |
|---|---|
| `value()` | The parsed response body, or `undefined` until data arrives |
| `isLoading()` | `true` while a request is in flight |
| `error()` | The error from a failed request, otherwise `undefined` |
The next chapter covers these in detail. For now, note that value() can be undefined, which is why the template uses user.value()?.name.
What happens at runtime
The request is not sent when the class is constructed. Angular runs it as part of its reactive scheduling, and it repeats the request whenever a signal read by the request function changes.
Common errors
- Forgetting
provideHttpClient(). The resource cannot create its request and reports an injection error. - Reading
value()as if it were always defined. Before the first response it isundefined. Use?.or an@ifblock. - Calling a signal outside the request function. If you write `
httpResource<User>(/api/users/${this.userId()})without the arrow function, you readuserIdonce and the URL never changes. Wrap the expression in() => ...`. - Expecting runtime type checking. The
<User>type argument is only a compile-time hint. The server could return anything. Validation is covered in a later chapter.
Exercise
In the UserComponent above, why does clicking "Next user" send a new request, and what would break if you replaced the arrow function with a template string that calls this.userId() directly?
Show answer
The request function reads the userId signal, so Angular tracks it as a dependency and re-runs the function when userId changes, which sends a new GET request. If you pass a plain string, this.userId() is read once when the resource is created. The URL never changes, so clicking the button would not send any new request.