Sub topic 13
Writes and mutations: use HttpClient
This chapter assumes you know Angular signals, basic httpResource usage, and HTTP methods. It covers when to leave httpResource and use HttpClient directly.
Reads belong to httpResource, writes do not
An httpResource describes data that should exist, derived from signals. Angular sends the request on its own whenever the signals it reads change. This fits reads such as GET. A write such as POST, PATCH or DELETE is an action. It should run once, when the user asks for it, and not each time a signal changes.
If you put a write in an httpResource request, the resource can resend it. That can happen when a signal changes or when you call reload(). A duplicate POST can create a second record.
| Task | Use |
|---|---|
| Load a user for display | `httpResource` |
| Search as the user types | `httpResource` |
| Create, update or delete data | `HttpClient` |
| Submit a form | `HttpClient` |
The request object does accept method and body. That support exists for reads that need them, such as a search endpoint that takes a POST body. It does not mean httpResource is the right tool for changing data.
Sending the write with HttpClient
Put writes in a service. The method returns an Observable. Nothing is sent until someone subscribes.
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class UserService {
private http = inject(HttpClient);
rename(id: number, name: string) {
return this.http.patch(`/api/users/${id}`, { name });
}
}HttpClient is the same service that httpResource uses internally, so interceptors such as authentication apply to both.
Refreshing the resource after a write
After the write succeeds, the resource still holds the old value. Call reload() on it to fetch fresh data.
import { Component, inject, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { UserService } from './user.service';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user-editor',
template: `
<p>{{ user.value()?.name }}</p>
<button (click)="rename()" [disabled]="saving()">Rename</button>
@if (saveError()) {
<p>Could not save the change.</p>
}
`,
})
export class UserEditorComponent {
private users = inject(UserService);
userId = signal(1);
user = httpResource<User>(() => `/api/users/${this.userId()}`);
saving = signal(false);
saveError = signal(false);
rename() {
this.saving.set(true);
this.saveError.set(false);
this.users.rename(this.userId(), 'New name').subscribe({
next: () => {
this.saving.set(false);
this.user.reload();
},
error: () => {
this.saving.set(false);
this.saveError.set(true);
},
});
}
}The component tracks its own saving and saveError signals. The resource's isLoading() and error() describe only the read, so they say nothing about the write.
Common mistakes
- Writing in the request function. A POST placed there can be sent again when a signal changes. Keep writes in
HttpClientcalls triggered by events. - Forgetting to subscribe.
http.patch(...)alone sends nothing. Subscribe, or convert it withfirstValueFrom. - Skipping the reload. The resource keeps showing the old value until you call
reload()or a signal it reads changes. - Reading the resource's status for the write. Track the write's progress separately.
Exercise
Exercise
A page loads a todo list with an httpResource. The user clicks "Delete" on one item. Which API sends the DELETE request, and what should the component do after it succeeds?
Show answer
Send the DELETE with HttpClient (for example this.http.delete('/api/todos/' + id)) from a service, and subscribe to it from the click handler. In the success callback, call reload() on the todo list resource so it fetches the list again without the deleted item.