Sub topic 5
Making the request depend on signals
This chapter assumes you can write Angular components, read signals in templates, and set up provideHttpClient(). You should also have declared a basic httpResource. Here you will see how signals drive the request.
The request function is a reactive computation
The first argument to httpResource is a function. Angular runs it inside a reactive context, so every signal you read in it becomes a dependency. When one of those signals changes, Angular runs the function again and sends a new request.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user',
template: `
<button (click)="next()">Next user</button>
@if (user.isLoading()) {
<p>Loading…</p>
}
@if (user.error()) {
<p>Could not load the user.</p>
}
<p>{{ user.value()?.name }}</p>
`,
})
export class UserComponent {
userId = signal(1);
user = httpResource<User>(() => `/api/users/${this.userId()}`);
next() {
this.userId.update((id) => id + 1);
}
}Reading this.userId() inside the arrow function is what creates the link. Clicking the button changes userId, and the resource issues GET /api/users/2. You never call a fetch method yourself.
What happens to the state
When a dependency changes, the resource moves back to a loading state. isLoading() becomes true, and value() holds no data for the new request until the response arrives (it is undefined, or your defaultValue if you set one). Your template should therefore handle the empty case, which is why the example uses user.value()?.name.
If the old request is still in flight when a new one starts, the resource cancels the old one. A slow response for user 2 cannot overwrite the result for user 3.
Combining several signals
You can read as many signals as you need. Use computed when the same derived value is used in several places.
import { signal, computed } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface Post {
id: number;
title: string;
}
export class PostsPage {
page = signal(1);
category = signal('news');
pageSize = 20;
path = computed(() => `/api/${this.category()}`);
posts = httpResource<Post[]>(
() => `${this.path()}?page=${this.page()}&size=${this.pageSize}`,
);
}Changing page or category triggers a request. Changing pageSize does not, because it is a plain property and not a signal. Angular cannot detect plain property changes.
Common errors
- Reading a signal outside the function. Code such as
const id = this.userId();placed before thehttpResourcecall runs once. The resource never reacts to later changes. Read the signal inside the request function. - Passing a string instead of a function. The first argument must be a function (or a request-returning function). A plain template string is evaluated once and is not accepted as a reactive source.
- Expecting a request on every set. Setting a signal to a value equal to its current one does not notify dependents, so no new request is sent.
- Forgetting the empty state. Between a change and the response,
value()is not the previous result. Guard against it in the template.
Exercise
Exercise
A component declares private id = 5; and item = httpResource<Item>(() => /api/items/${this.id});. A button handler runs this.id = 6. Why is no new request sent, and how do you fix it?
Show answer
id is a plain property, not a signal, so the request function has no reactive dependency to track and never re-runs. Declare id = signal(5), read it as this.id() inside the request function, and change it with this.id.set(6). The resource then cancels any in-flight request and sends GET /api/items/6.