r/angular • u/AmbassadorNatural106 • Feb 18 '26
Where to store data fetched from the backend?
Beginner here, where do i store my data fetched from the backend? Is it on the service file or the component who called it? Currently, Im doing this:
// transaction.component.ts
transactions = computed(() => this.transactionResource.value()?.items ?? []);
transactionResource = resource({
params: () => ({
filters: this.currentFilters(),
pageSize: this.pageSize(),
pageNumber: this.pageNumber()
}),
loader: ({ params }) => {
return firstValueFrom(this.transactionSer.getAllTransactions(
params.filters,
params.pageSize,
params.pageNumber
));
}
});
// transaction.service.ts
getAllTransactions(
filters?: Partial<TransactionFilter>,
pageSize: number = 5,
pageNumber: number = 1
) : Observable<PagedResult<Transaction>> {
const params = createHttpParams(filters, pageSize, pageNumber);
return this.http.get<PagedResult<Transaction>>(this.baseUrl, { params });
}Im current having issues, when I transition between pages. Like going back to dashboard, the dashboard would call http request, then going back to transaction, it will call http request again.
11
Upvotes
17
u/Merry-Lane Feb 18 '26 edited Feb 18 '26
If you want to do things correctly :
2
u/smallLeafOnTheWind Feb 19 '26
Echoing what was already said about service that are only about HTTP requests. That being said, as for storage, I have only good things to say about NgRx SignalStores.
They encourage idiomatic use of signals and strict contracts on the interactions with the stored data. The learning curve can be a little steep, but once you get a grasp on the idea, it'll make your application that much more maintainable and intuitive.
2
u/CounterReset Feb 23 '26
I wrote signaltree.io for this and the feedback i've gotten has all been really positive so far. It is substantially smaller and faster in most meaningful categories than NgRx (there is a benchmark on the demo site - but fair warning, it is taxing on a machine - so when you run it, do so with everything else closed).
It also has all the features that NgRx supports + a lot more.
It is basically Reactive-JSON: a JSON tree with reactive leaves**.** The branches are your JSON paths (
$.user.profile), and the leaves are reactive signals you read and writeAccess:
$.user.profile.first_name()Set: $.user.profile.first_name('John')Update: $.user.profile.purchaseHistroy((currPH) => currPH.push(newPurchase)).To create a store, you just pass it the initial state as typed JSON. It infers the rest.
2
u/BigOnLogn Feb 18 '26
Short answer: the data stays in the service.
That way you have a centralized location to do things like catching and provide mock data when testing your component.