Sub topic 1
Why resources: async data as signals
Prerequisites
This chapter assumes you know the following:
- TypeScript fundamentals.
- Angular components and templates.
- Angular signals:
signal,computed, and reading signals in templates. - Basic HTTP concepts: methods, query parameters, headers, and status codes.
You do not need to know httpResource yet. This chapter explains the idea behind it. Later chapters cover the API.
The problem with async data
Signals hold values that are available right now. Data from a server is not. A request takes time, it can fail, and its answer can arrive after the input that triggered it has already changed.
Without a dedicated tool, you track each of these cases by hand. A typical component ends up with fields like these:
import { Component, OnInit, inject, signal } from '@angular/core';
import { HttpClient } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-manual-user',
template: `
@if (loading()) {
<p>Loading…</p>
}
@if (error()) {
<p>Could not load the user.</p>
}
<p>{{ user()?.name }}</p>
`,
})
export class ManualUserComponent implements OnInit {
private http = inject(HttpClient);
user = signal<User | undefined>(undefined);
loading = signal(false);
error = signal(false);
ngOnInit() {
this.loading.set(true);
this.http.get<User>('/api/users/1').subscribe({
next: (u) => {
this.user.set(u);
this.loading.set(false);
},
error: () => {
this.error.set(true);
this.loading.set(false);
},
});
}
}This works for one fixed request. It breaks down when the URL depends on another signal. You must watch that signal, start a new request, reset the flags, and make sure an old response does not overwrite a newer one. Each component repeats this code, and each copy can get it wrong.
What a resource is
A resource is an object that loads asynchronous data and exposes its state as signals. You describe what to load, and the resource handles the loading.
A resource has two parts:
- A request function that reads signals and says what to fetch.
- A set of signals that report the outcome.
When a signal read inside the request function changes, the resource starts a new load. It also discards the result of any load that is now out of date.
The signals a resource exposes include:
| Signal | Meaning |
|---|---|
| `value()` | The latest loaded data, or `undefined` if there is none |
| `status()` | The current state, such as idle, loading, resolved, or error |
| `error()` | The error from a failed load, or `undefined` |
| `isLoading()` | `true` while a load is in progress |
Because these are ordinary signals, templates and computed values can read them and update automatically. Later chapters cover each one in detail.
Where httpResource fits
Angular's general resource function accepts any asynchronous loader, such as a function that returns a promise. httpResource is a specialization for HTTP. It builds on HttpClient, so it uses the same interceptors and the same backend configuration as the rest of your application.
You give httpResource a URL or request object, and it returns a resource. The manual component above shrinks to a declaration:
import { Component } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user',
template: `
@if (user.isLoading()) {
<p>Loading…</p>
}
@if (user.error()) {
<p>Could not load the user.</p>
}
<p>{{ user.value()?.name }}</p>
`,
})
export class UserComponent {
user = httpResource<User>(() => '/api/users/1');
}The component no longer subscribes, sets flags, or handles cleanup. The next chapters show how to set up HttpClient, then how to make the URL depend on signals.
How the pieces connect
The following diagram shows the flow. A signal change triggers the request function, the resource loads, and the state signals update the template.
When to use a resource
Use a resource to read data that your UI displays and that depends on signals. Reads fit the model because repeating a read is safe, and the latest result is the one that matters.
Writes are different. Creating, updating, or deleting data are actions with side effects. A later chapter explains why those belong in HttpClient directly.
Exercise
Name two problems a resource solves that a manual subscribe approach leaves to you when the URL depends on a signal.
Show answer
Any two of these: starting a new request when the signal changes, keeping loading and error flags correct, and preventing an old response from overwriting a newer one.
Exercise
Why can a template or computed value read value() and isLoading() directly?
Show answer
Both are signals, so Angular tracks reads of them and updates the template or computed value when they change.