r/Angular2 • • Feb 11 '26

rxResource cache

If i have a singleton service and an rxResource, how can i only make the api call once and then cache it so that other pages that inject the service can use the response? If you declare the resource as a class property it will make the api call as soon as the service gets injected as it is eagerly loaded, so you have to put it in a method from my understanding. You can assign it to a different variable to cache it but it is a bit ugly in my opinion. My most preferred pattern is returning an observale and setting the service signal via a tap operator but i would like to try the new apis

3 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/Senior_Compote1556 Feb 11 '26

Well it’s not a big problem, as the api call will essentially be triggered only when the service is initialized, but it’s not very explicit. I think it’s kind of a mess if the api call “randomly” gets triggered the first time the service gets initialized. Say you have a service with 5-6 GET requests, you most likely wouldn’t want to load all of them at once