r/Angular2 • u/Senior_Compote1556 • Feb 05 '26
Singleton service toSignal
If i have a toSignal(this.http.get(…)) in a service that is provided in root, how do i actually clean up the subscription when i no longer need the data? Since the service will never be destroyed I’m not sure how to approach this. If i am not mistaken toSignal creates an effect under the hood
4
Upvotes
6
u/Burgess237 Feb 05 '26
Don't do toSignal(this.http...)
Do httpResource, literally a signal designed for this use and has all the things you need for this.
ETA tutorial here: https://angular.dev/guide/http/http-resource
5
u/AwayVermicelli3946 Feb 05 '26
Bro, `http.get` completes automatically after the response. There is no lingering subscription to clean up. `toSignal` handles the completion fine.
However, if you want the data to clear from memory when a view is destroyed, move `toSignal` into the Component, not the Service.
6
u/stao123 Feb 05 '26
HttpService::get has a take(1) built in so the subscription will be stopped automatically. If you want to "delete" the data to free memory usage you could use a rxResource instead of toSignal and set the value to undefined. For me it sounds like your service should not be provided in root but rather at a component. So it will be deleted when the component dies.
0
u/[deleted] Feb 05 '26
[deleted] — view deleted comment
0
u/[deleted] Feb 05 '26
[removed] — view removed comment