Sub topic 6
Waiting for input: returning undefined
This chapter assumes you know TypeScript, Angular components and templates, and signals (signal, computed, reading signals in templates). It also assumes you know basic HTTP: query parameters and status codes. You should already have declared a basic httpResource and made it depend on a signal.
The problem: a request you don't want yet
An httpResource sends its first request as soon as it is created. For many screens that is wrong. A search box starts empty, and a detail page may not know its id yet. Sending GET /api/products?q= for an empty query wastes a call and may return an error or the whole catalog.
You can't skip creating the resource, because you declare it once, as a class field. Instead, you tell it to wait.
Returning undefined
The request function you pass to httpResource can return undefined. When it does, the resource stays idle and sends no request. When a signal the function reads changes and the function returns a real URL or request object, the resource sends the request.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface Product {
id: number;
title: string;
}
@Component({
selector: 'app-search',
template: `
<input (input)="query.set($any($event.target).value)" />
<ul>
@for (p of results.value(); track p.id) {
<li>{{ p.title }}</li>
}
</ul>
`,
})
export class SearchComponent {
query = signal('');
results = httpResource<Product[]>(
() => {
const q = this.query();
if (!q) {
return undefined;
}
return { url: '/api/products', params: { q, limit: '10' } };
},
{ defaultValue: [] },
);
}The function reads this.query(), so the resource tracks that signal. While the input is empty, the function returns undefined and nothing is sent. After the user types, the function returns a request object and the resource sends GET /api/products?q=...&limit=10.
The defaultValue: [] option makes value() return an empty array while the resource is idle or loading. The template can loop over results.value() without a null check.
What the resource reports while idle
An idle resource is not loading and has no error. Its status() is 'idle', and isLoading() is false. Its value() is undefined, or your defaultValue if you set one.
| Situation | status() | isLoading() | value() |
|---|---|---|---|
| Request function returns `undefined` | `idle` | `false` | `undefined` or the default |
| Request in flight | `loading` | `true` | previous value or default |
| Response arrived | `resolved` | `false` | the parsed body |
| Request or parse failed | `error` | `false` | `undefined` or the default |
Use this to show the right message. Show a hint such as "Type to search" when the status is idle, and a spinner only when isLoading() is true.
Common errors
- Returning an empty string instead of
undefined. An empty URL is not "no request". Returnundefinedexplicitly. - Reading signals after the early return. Signals the function never reads are not tracked. In the example,
this.query()is read before thereturn undefined, so the resource still reacts when the query changes. If you put the first signal read behind a condition that depends on other state, make sure at least one tracked signal can change and wake the function up. - Assuming
value()is defined. WithoutdefaultValue,value()isundefinedwhile idle. Useuser.value()?.nameor set a default. - Showing "Loading…" for an idle resource. Check
isLoading(), not!value(). An empty value can mean idle, loading, or an empty result.
Exercise
A detail component has id = signal<number | null>(null). Write the request function for an httpResource that loads /api/orders/{id} only after id holds a number. What are status() and isLoading() before that?
Show answer
() => { const id = this.id(); return id === null ? undefined : /api/orders/${id}; }. While id is null, the function returns undefined, so status() is 'idle' and isLoading() is false. Once id.set(5) runs, the function returns the URL and the resource sends the request.
Exercise
Why does the search example set defaultValue: [], and what would the template see without it while the query is empty?
Show answer
With the default, results.value() is an empty array, so @for renders nothing. Without it, value() is undefined, and the template needs a guard such as results.value() ?? [].