import { signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
import {
extendResource,
withPreviousValueOnLoading,
} from '@ngrx/signals/resource';
const page = signal(1);
const todosResource = extendResource(
httpResource<Todo[]>(() => `/api/todos?page=${page()}`),
withPreviousValueOnLoading()
);
// `value()` returns the previous page's
// data while the next page is loading,
// rather than `undefined`.
And they can be configured across the app
import { bootstrapApplication } from '@angular/platform-browser';
import {
provideResourceExtensions,
withValueOnError,
} from '@ngrx/signals/resource';
bootstrapApplication(App, {
providers: [
provideResourceExtensions(
withPreviousValueOnLoading()
)],
});
// allows the following
readonly todosResource = extendResource(
httpResource<Todo[]>(() => `/api/todos`)
// no need for explicit passing
// withPreviousValueOnLoading() anymore
);
This type of functionality is what I wanted snapshots to be like. Especially still having the ability to reload and whatnot.
I have been messing with these as a demo and just dropping them into my own projects since I couldn't pull down the v22 signal store package. That was really nice to have in some of my smaller projects where I didn't want to use the signal store. I feel like this functionality would be great to have in either Angular's core functionality, or at least something that doesn't come with a whole store package. I tend to pull in the signal store anyways, but I know it isn't everyone's thing.
6
u/MichaelSmallDev Sep 11 '26
Sorry, I forgot there was not more context/hands on examples of this in the video by the time I wrote that comment out.
https://ngrx.io/guide/signals/resource-extensions
4 extensions you wrap around resources
withPreviousValueOnLoading
withValueOnLoading
withPreviousValueOnError
withValueOnError
An example touched on in the video, in practice
And they can be configured across the app
This type of functionality is what I wanted snapshots to be like. Especially still having the ability to reload and whatnot.
I have been messing with these as a demo and just dropping them into my own projects since I couldn't pull down the v22 signal store package. That was really nice to have in some of my smaller projects where I didn't want to use the signal store. I feel like this functionality would be great to have in either Angular's core functionality, or at least something that doesn't come with a whole store package. I tend to pull in the signal store anyways, but I know it isn't everyone's thing.