Sub topic 9
Refreshing data with reload()
This chapter assumes you can create a resource with a loader and read its value(), status(), and isLoading() signals. Here you will re-run a loader on demand, without changing any of its inputs.
When params are not enough
A resource re-runs its loader when a signal read inside params changes. Sometimes the inputs stay the same but the server data has changed. Examples are a "Refresh" button, a poll timer, or a save that happened elsewhere. For these cases every resource has a reload() method.
import { Component, resource } from '@angular/core';
interface Todo {
id: number;
title: string;
}
@Component({
selector: 'app-todo-list',
template: `
<button (click)="refresh()" [disabled]="todos.isLoading()">Refresh</button>
@if (todos.status() === 'reloading') {
<p>Updating...</p>
}
@if (todos.error()) {
<p>Could not load todos.</p>
}
<ul>
@for (todo of todos.value(); track todo.id) {
<li>{{ todo.title }}</li>
}
</ul>
`,
})
export class TodoListComponent {
readonly todos = resource({
defaultValue: [] as Todo[],
loader: async ({ abortSignal }) => {
const res = await fetch('/api/todos', { signal: abortSignal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as Todo[];
},
});
refresh() {
this.todos.reload();
}
}The loader runs once when the component is created. Each click on the button runs it again with the same params.
Loading versus reloading
A resource that loads for the first time, or because its params changed, has the status loading. The previous value is discarded, so value() falls back to undefined or your defaultValue.
A resource that is refreshed through reload() has the status reloading. It keeps the last good value while the new request is in flight. The list in the example stays on screen and the template can show a small "Updating..." hint instead of a full-page spinner.
isLoading() is true for both statuses. Use status() when you need to tell them apart.
What reload() returns
reload() returns a boolean. It is true when a new load started and false when nothing happened. The call does nothing in two cases:
- The resource is idle, because
paramsreturnedundefined. There is no input to load with. - A load is already running. Calling
reload()again does not queue a second request.
You can ignore the return value for a simple button. Check it when your code must know whether a request really started.
refresh() {
const started = this.todos.reload();
if (!started) {
console.warn('Reload skipped: resource is idle or already loading');
}
}Retrying after an error
A resource in the error state can be reloaded the same way. This gives you a retry button with no extra state:
@if (todos.error()) {
<p>Could not load todos.</p>
<button (click)="todos.reload()">Try again</button>
}Common mistakes
- Reloading to react to a signal. If the refresh depends on a signal, such as a filter, read that signal in
params. Usereload()only for refreshes with no input change. - Expecting a spinner on every refresh.
status()isreloading, notloading. A template that only checksstatus() === 'loading'shows nothing during a refresh. - Calling reload() in a constructor or effect. The resource already loads when it is created. An extra call is redundant, and an effect that calls it can loop.
Exercise
A resource shows a list. The user clicks a "Refresh" button that calls reload(). During the request, what are status() and value(), and what does isLoading() return?
Show answer
status() is 'reloading', value() still holds the previous list, and isLoading() returns true. Only a first load or a params change gives the status 'loading' and drops the old value.
Exercise
Your resource has params: () => ({ page: this.page() }). Should the "Next page" button call reload()?
Show answer
No. Update the page signal. Because params reads it, the resource reloads on its own and cancels any older request. reload() is for refreshing with unchanged params.