Sub topic 6
Reading value, status, and error
This chapter assumes you can create a resource with a params function and a loader, as in the earlier chapters. A resource exposes its state through signals. Reading those signals correctly is what keeps a template from showing stale or broken data.
The state signals
A resource reference has these read-only signals:
value()holds the latest loaded data, orundefinedif there is none. If you setdefaultValue, it holds that instead.status()holds one of six states, listed in the table below.error()holds whatever the loader threw, orundefined.isLoading()istruewhile a load is running, whether it is a first load or a reload.hasValue()istruewhenvalue()holds real data. It also narrows the type ofvalue()in TypeScript.
| Status | Meaning | Value |
|---|---|---|
| `idle` | `params` returned `undefined`, so no load was started | `undefined` or `defaultValue` |
| `loading` | First load, or a load after `params` changed | `undefined` or `defaultValue` |
| `reloading` | A load started by `reload()` | Previous value is kept |
| `resolved` | The loader finished | The loaded data |
| `error` | The loader threw or rejected | Not usable |
| `local` | You changed the value with `set()` or `update()` | Your local value |
How the states connect
The difference between loading and reloading matters for the interface. After params change, the old value no longer matches the new input, so the value is cleared. After reload(), the input is the same, so the old value stays visible while fresh data arrives.
Reading the signals
The component below shows three ways to use these signals: an isLoading flag, a hasValue() guard, and a status() label.
import { Component, resource, signal } from '@angular/core';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user-status',
template: `
<p>Status: {{ user.status() }}</p>
@if (user.error()) {
<p>Could not load the user.</p>
} @else if (user.hasValue()) {
<p>{{ user.value().name }}</p>
@if (user.isLoading()) {
<p>Refreshing...</p>
}
} @else if (user.isLoading()) {
<p>Loading...</p>
}
<button (click)="userId.set(userId() + 1)">Next user</button>
`,
})
export class UserStatusComponent {
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 template checks error() first. Then it checks hasValue(). Only after that does it fall back to isLoading(). Because hasValue() is checked before isLoading(), a reload keeps the old data on screen and adds a "Refreshing..." line. A first load, which has no value yet, shows "Loading...".
Choosing which signal to read
- Use
isLoading()for spinners and for disabling buttons. - Use
hasValue()before you readvalue()as data. - Use
error()to decide whether to show an error message. - Use
status()when you need to tellidlefromloading, orreloadingfromloading.
Exercise
A user clicks a "Refresh" button that calls reload(). The data was already loaded. Which status does the resource report while the request runs, and is value() still the old data?
Show answer
The status is reloading. value() keeps the previous data until the new request finishes, and isLoading() is true. If the params had changed instead, the status would be loading and the value would be cleared.