Sub topic 4
Reading value, status and errors in templates
This chapter assumes you can write an Angular component with a signal and a template, and that you have declared a basic httpResource like httpResource<User>(() => '/api/users/1'). Here you learn how to read the state of that resource in a template.
What a resource exposes
An httpResource is a set of signals. You read each one by calling it, so templates update when it changes.
| Signal or method | Meaning |
|---|---|
| `value()` | The latest parsed response, or `undefined` if there is none yet |
| `status()` | One of the status strings in the next section |
| `error()` | The error from the last failed request, or `undefined` |
| `isLoading()` | `true` while a request is in flight, including a reload |
| `hasValue()` | `true` when `value()` holds real data |
The status values
status() tells you exactly where the resource is in its life cycle.
idle: no request is active and there is no value. This happens when the request function returnsundefined.loading: the first request, or a request after the inputs changed.reloading: a request started byreload(), with the old value still available.resolved: the request succeeded andvalue()holds the result.error: the request failed anderror()holds the cause.local: someone set the value directly withvalue.set().
Rendering every state
A template usually handles four cases: loading, error, data, and nothing yet. The @if and @else if chain below shows each one.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user-card',
template: `
@if (user.error(); as err) {
<p>Could not load the user.</p>
<button (click)="user.reload()">Try again</button>
} @else if (user.hasValue()) {
<h2>{{ user.value().name }}</h2>
@if (user.isLoading()) {
<small>Updating…</small>
}
} @else if (user.isLoading()) {
<p>Loading…</p>
}
<p>Status: {{ user.status() }}</p>
`,
})
export class UserCardComponent {
userId = signal(1);
user = httpResource<User>(() => `/api/users/${this.userId()}`);
}The order of the branches matters, for two reasons.
- The error check comes first. In current Angular versions, reading
value()while the resource is in theerrorstate throws. Checkingerror()orhasValue()before you touchvalue()avoids that. hasValue()comes beforeisLoading(). During a reload the resource still has the old value. The page keeps showing it, with a small "Updating…" note, instead of flashing a blank loading screen.
hasValue() also narrows the type. Inside the @if, TypeScript treats user.value() as User instead of User | undefined, so you don't need ?..
Common mistakes
- Using
isLoading()as the only guard. Before the first request starts, and while the resource isidle,isLoading()isfalseandvalue()isundefined. Your template shows nothing. Checkstatus()orhasValue()too. - Reading
value()unguarded in the error state. This throws. Guard it withhasValue(). - Expecting
error()to be an HTTP response. It holds the error from the failed request. For HTTP failures this is usually anHttpErrorResponse, whosestatusproperty is the HTTP status code. Cast or check its type before you read that property. - Expecting the error to clear immediately.
error()clears when the next request moves the resource out of theerrorstate, not before.
Exercise
Exercise
A user clicks a "Refresh" button that calls user.reload(). The resource already has data. Which status does it have while the new request runs, what does isLoading() return, and does hasValue() stay true?
Show answer
The status is reloading. isLoading() returns true. hasValue() stays true because the previous value is kept until the new response arrives, so the template can keep showing the old data with an "Updating…" note.
Exercise
Why does the example template check user.error() before it reads user.value()?
Show answer
In the error state, reading value() throws in current Angular versions. Checking error() first, or guarding with hasValue(), means the template never reads value() in a state where it would fail.