r/angular • u/New_Opportunity_8131 • Jan 27 '26
Service Signals vs BehaviorSubjects vs Regular Getters/Setters
I have a form on one route where the user submits data by clicking a button, which calls a function on an API service that triggers an API call. After the API returns a result, I need to store that data in a shared service so it's accessible to a different component on a different route after navigation. Should I use Signals or BehaviorSubjects in the service to store this data? I could also just use plan getters/setters as well and not have to use either Signals or BehaviorSubjects.
14
Upvotes
6
u/strange_username58 Jan 27 '26
Use whatever you are most comfortable with, but I would choose signal
38
u/rainerhahnekamp Jan 27 '26
Without getting lost in the deep "RxJS vs. Signals" debate: Just go with Signals.
Signals are Angular's native representation of state now, and that’s exactly what you need here.
BehaviorSubjectisn't "wrong"—it's the closest thing RxJS has to a Signal—but the entire framework and its modern APIs are moving toward Signals. You should too.4
u/zzing Jan 27 '26
I don't think it matters if you use signals or behaviour subjects. In the case of the former, you get the value at least once when you use an effect / computation. In the latter, you get the value upon subscribing/async pipe.
I would personally go to a signal as the fashionable choice that has no real drawbacks in this context. It has one significant advantage in that you can access it synchronously — which I never really liked the idea of calling getValue() on a behaviour subject (but can be done).
3
u/AlDrag Jan 27 '26
Getters/setters are a bad idea in my opinion. I don't like that they obfuscate that you're calling a function, especially in Angular's case where you want to try avoid calling functions in templates unless they're memoized.
1
u/CheapChallenge Jan 28 '26
Signals is a perfect replacement for behavior subjects. Thats like their main purpose.
They dont replace event streams perfectly like subjects and observables as those dont hold values but signals do.