AI Hints
Home›Angular Tutorial›httpResource, Interceptors and Practice: A Books Client
Angular · Lesson 97 of 120

httpResource

Requests as signals.

⏱ 2 min read▶ Video at 1:57:07🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Subscribing, storing the result, tracking loading, and catching errors, is a lot of code for every request. HTTP resource does all of it for us, with signals.

  • Subscribe, store, load, catch
  • A lot of code
  • httpResource

httpResource

HTTP resource takes a function, that returns the URL. It sends the request for us, and gives back signals. Value holds the data. Is loading is true while waiting. And error holds any failure.

keyvalue
httpResource<T>(() => url)make a request
.value()the data
.isLoading()waiting?
.error()what went wrong

Books as a resource

One line creates the resource. Default value is an empty list, so the template always has an array. The template reads is loading, error, and value. Run it. One request was sent, and the books appeared, with no subscribe at all.

app.tstypescript
import { Component } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { Book } from './book';

@Component({
  selector: 'app-root',
  template: `
    @if (books.isLoading()) { <p>Loading...</p> }
    @if (books.error()) { <p class="error">Could not load the books.</p> }
    @for (b of books.value(); track b.id) { <p>{{ b.title }} · {{ b.author }}</p> }
  `,
})
export class App {
  books = httpResource<Book[]>(() => '/api/books', { defaultValue: [] });
}
Output
API GET /api/books → 200
Also uses book.ts
export interface Book { id: number; title: string; author: string; price: number; }
Also uses _apilog
1

A reactive URL

The URL function reads the q signal. So whenever q changes, the resource sends a new request, by itself. The input sets q on every key press. We type austen. Run it. Nearly every key press sent a new request, and the last one found only Emma. In module twelve, we'll wait for the user to stop typing.

app.tstypescript
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { Book } from './book';

@Component({
  selector: 'app-root',
  template: `
    <input (input)="q.set($any($event.target).value)" placeholder="Search">
    @for (b of books.value(); track b.id) { <p>{{ b.title }} · {{ b.author }}</p> }
  `,
})
export class App {
  q = signal('');
  books = httpResource<Book[]>(() => `/api/books?q=${this.q()}`, { defaultValue: [] });
}
Output
API GET /api/books?q= → 200
API GET /api/books?q=a → 200
API GET /api/books?q=au → 200
API GET /api/books?q=aus → 200
API GET /api/books?q=auste → 200
API GET /api/books?q=austen → 200
Also uses book.ts
export interface Book { id: number; title: string; author: string; price: number; }
Also uses actions.json
[{"type": "input", "text": "austen"}, {"wait": 900}]
Also uses _apilog
1

Summary

HTTP resource sends requests for you, and gives value, is loading, and error, as signals. It even reloads when its URL signals change. Next, interceptors.

Key takeaways
  • httpResource()
  • value, isLoading, error
  • Reactive URLs
Next up: Interceptors — code for every request