Recently some co-workers and I were having a discussion as to whether or not AngularJS services should have state or not. We came up with some arguments for and against it and I wanted to get additional thoughts and feedback on the subject. In my searching I found this but there doesn't seem to be any clear best-practice mentioned. In the none client-side world a service should never hold state, but I am starting to think that it might be acceptable client-side because its a different problem.
Reasons for services holding state:
Reasons for services to not hold state:
One way that might address #2 in the "for services holding state" section would be to have an appState object set on the rootScope that contains the current state of the application. Then all the state would be gathered in one location and then you just pull what you need out of it in your service. I found this and wondered
It's usually a good idea to have components stateless and store the state in a service, especially in components added by the router, so that navigating away and later back to a route, doesn't drop the data. Therefore to your question: Services are not supposed to be stateless. They often are, but it's not required.
Here, the application state is stored in angular services. But, the actual problem is if we refresh the page we will lose the application state stored in the angular service. So, If we use the angular services to keep the application state, it is necessary to use a backend to save the application state.
Angular Services They contain methods that maintain data throughout the life of an application, i.e. data does not get refreshed and is available all the time. The main objective of a service is to organize and share business logic, models, or data and functions with different components of an Angular application.
When it comes to Angular, NgRx and NGXS are the two most-used libraries for state management, and they have some unique features that make developers' work easy.
It would probably depend on what you mean by "state", but in many cases I think the answer would be yes: services should hold state.
For example, if you have a service that is responsible for communication with an API, that service could hold the authentication state.
By the way, I'm not sure how much idempotence matters for AngularJS services - they're singletons and so inherently have some state. You could (and in some cases must) create idempotent methods on the service, but that's a separate issue.
In AngularJS, services are passed in via factory function. And basically they are objects that can contain some state (e.g. for caching or storing data needed for performing their actions).
One good solution that can take both cons of having/not having state is when service (that could be actually function) that return object that contain state.
Take a look at the $http
service: you can get instance of this service calling
var x = $http({url:'...'});
And then call by
var result = x.get() //actually `$http.get` is shortcut of this operation
Same with ngResource
: using service you get object with some state that can perform desired actions.
So basically I think that is the best option: from one point you avoid 'side effects' by moving state that could be modified by actions into separate object, not stored in service itself, but can have specific state in that object to be able to store custom info (like auth information etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With