Sub topic 3
Your first resource
This chapter assumes you know TypeScript with async/await, Angular components and templates, and the basics of signals (signal, computed, effect). It covers the smallest useful resource(): what you pass in, what you get back, and when the loader runs.
What a resource is
A resource is an object that wraps one asynchronous read, such as an HTTP request. It runs an async function, the loader, and exposes the outcome as signals. You no longer write your own loading, error, and data fields and update them by hand.
You create one with the resource() function from @angular/core. It takes an options object with two main parts:
params: a function that reads signals and returns the input for the loader.loader: an async function that receives that input and returns the data.
A minimal example
The component below loads a user by ID. The button changes the ID signal.
import { Component, resource, signal } from '@angular/core';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user',
template: `
<button (click)="userId.set(userId() + 1)">Next user</button>
<p>{{ user.value()?.name }}</p>
`,
})
export class UserComponent {
readonly userId = signal(1);
readonly user = resource({
params: () => ({ id: this.userId() }),
loader: async ({ params, abortSignal }) => {
const res = await fetch(`/api/users/${params.id}`, { signal: abortSignal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as User;
},
});
}The type of user.value() is inferred from the loader's return type, so it is User | undefined. The value is undefined until the first load finishes.
How it runs
The params function is reactive, like a computed. Angular tracks every signal it reads. Here that is userId. When userId changes, params produces a new result and the loader runs again with it.
The loader starts when the resource is created, not when the template first reads value(). The loader receives an object with these properties:
| Property | Meaning |
|---|---|
| `params` | The value your `params` function returned. |
| `abortSignal` | An `AbortSignal` that fires when this request is superseded. |
Passing abortSignal to fetch, as above, lets the browser cancel the old request when a newer one starts. The "Cancelling stale requests" chapter covers this in detail.
Common mistakes
- Reading signals inside the loader. Only signals read in
paramstrigger a reload. Read everything the request depends on inparamsand pass it through, as the example does withparams.id. - Forgetting the status check on
fetch.fetchonly rejects on network failure. A 404 or 500 resolves normally, so throw an error yourself whenres.okis false. - Assuming
value()is always defined. Before the first load, and after an error, it holds no data. Use?.or adefaultValue.
Exercise
In the example, you add console.log(this.userId()) inside the loader but no other change. You then change a different signal, theme, that is read only inside the loader body. Does the loader re-run when theme changes? Why or why not?
Show answer
No. Only signals read inside params are tracked. Reading theme inside the loader does not make the resource reactive to it. To reload on theme changes, read it in params and include it in the returned object.