Sub topic 8
Providing a default value
This chapter assumes you can declare an httpResource and read its value(), isLoading() and error() signals in a template. It also assumes you know the idle pattern, where the request function returns undefined.
The problem: value() can be undefined
Until the first response arrives, a resource has no data. For a type T, value() therefore has the type T | undefined. Every read needs a guard, such as user.value()?.name or an @if block.
The defaultValue option removes the guard. You pass a value of type T. The resource returns it whenever it has no real data, and value() is typed as plain T.
Declaring a default
You pass defaultValue in the options object, which is the second argument of httpResource:
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface Product {
id: number;
title: string;
}
@Component({
selector: 'app-product-list',
template: `
<input (input)="query.set($any($event.target).value)" />
<p>{{ products.value().length }} results</p>
<ul>
@for (p of products.value(); track p.id) {
<li>{{ p.title }}</li>
}
</ul>
`,
})
export class ProductListComponent {
query = signal('');
products = httpResource<Product[]>(
() => {
const q = this.query();
return q ? { url: '/api/products', params: { q } } : undefined;
},
{ defaultValue: [] },
);
}products.value().length needs no ?., and the @for loop can iterate over value() directly. Without defaultValue, the TypeScript compiler would reject both.
When the default is returned
The default is the value of the resource whenever no response data is available. This happens in three situations:
- The resource is idle because the request function returned
undefined. - The first request is still loading.
- The request failed.
The diagram shows how the resource chooses what value() returns.
The default is not server data
A default of [] looks the same as a real response that contains no products. The template cannot tell "nothing loaded yet" from "the server found nothing" by reading value() alone. Check the other signals when the difference matters:
import { Component } from '@angular/core';
import { httpResource } from '@angular/common/http';
@Component({
selector: 'app-tags',
template: `
@if (tags.isLoading()) {
<p>Loading…</p>
} @else if (tags.error()) {
<p>Could not load tags.</p>
} @else if (tags.value().length === 0) {
<p>No tags yet.</p>
} @else {
<ul>
@for (t of tags.value(); track t) {
<li>{{ t }}</li>
}
</ul>
}
`,
})
export class TagsComponent {
tags = httpResource<string[]>(() => '/api/tags', { defaultValue: [] });
}The empty-state message appears only after loading finishes without an error.
Common mistakes
- Choosing a default that can pass for real data. A user object with
name: ''renders as a blank name. A list or a count is usually safer. - Mutating the default. The same default object is returned on every read. Treat it as read-only, and copy it before changing it.
- Expecting the default to hide errors. After a failed request,
value()returns the default, buterror()still holds the failure. Show an error message fromerror(). - Using a default to skip a request. The default does not stop requests. Only returning
undefinedfrom the request function does.
Exercise
A resource is declared as httpResource<number[]>(() => '/api/scores', { defaultValue: [] }). The server is unreachable, so the request fails. What does scores.value() return, and how can the template show an error message?
Show answer
value() returns the default, an empty array [], because no response data exists. The failure is not hidden. scores.error() holds the error, so the template can use @if (scores.error()) { ... } to show a message.