httpResource is an experimental API introduced in Angular v. 19.2.x and provides a new strategy for managing server communication in Angular applications.
In a previous lesson we have already discussed about the new features introduced by resource and rxResource and I invite you to read it in case you haven’t.
In this example we discuss a possible strategy to make parallel and sequential calls.
Suppose we have the id of a post, in our case postId which is equal to 1.
The post id could be retrieved from the browser’s url or passed to a comment via input properties.
So we can do this sequence of operations:
GET post info
The first httpResource has postId() as dependency so it will be invoked automatically when the component is mount or every time it changes:
GET user info and all the comments of the post
The second and third httpResource depend on the previous one and when post.value() changes they will be automatically invoked.
In fact we retrieve the userId from the previous call and we use it to compose new URLs.
Here the result :
src/app.component.ts
Multiple Http Requests (with increment)
This example is essentially identical to the previous one except that we increment the postId to load new data when a button is clicked
We increase the value of postId by 5 since the userId changes every 10 posts.
The new #angular httpResource API allows to directly integrate Schema Validations using libraries such as Zodtext or Valibottext .
What is it for?
Imagine that your endpoint at some point no longer returns the name property but has changed to title.
Thanks to the definition of a schema you can be immediately alerted and generate an exception.
How?
create your scheme definition:
use the parse property of httpResource passing the schema:
In previous paragraph we have seen how an httpResource can depend on signals.
What if we wanted to use it with the Reactive Forms that currently (v.19) only expose Observables?
We can easily create a signal from an Observable, using toSignal:
src/app.component.ts
In this way, we could have an httpResource that depends on query(), which will be updated whenever the user writes into a reactive input or form:
src/app.component.ts
Since the query signal is based on an Observable, we can then exploit a whole series of RxJS operators very useful for forms:
src/app.component.ts
The advantage of using a debouce strategy is to avoid that an httpResource based on this signal will be invoked at each typing but only when the user finishes typing.
Final Source Code
src/app.component.ts
More Examples coming soon...
We have prepared 10 snippets with several examples that integrate httpResource in different scenarios, combining Signals with RxJS, ngModel, Reactive Forms and much more.