Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should an Angular service have state?

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:

  1. The service isn't going to be accessed by multiple threads. Each browser will have its own instance of the service.
  2. Allows the service to hold the state only it cares about instead of storing it in the rootScope. encapsulates

Reasons for services to not hold state:

  1. Services are no longer idempotent. Calling functions may change state and therefore may have different results when calling it based upon the state of the service.
  2. I would think that overall this would be easier to test.

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

like image 294
testing123 Avatar asked Jan 09 '13 16:01

testing123


People also ask

Should Angular services be stateless?

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.

Is state management necessary in Angular?

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.

What should be in an Angular service?

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.

Does Angular have built in state management?

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.


2 Answers

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.

like image 178
Josh David Miller Avatar answered Sep 21 '22 05:09

Josh David Miller


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).

like image 27
Valentyn Shybanov Avatar answered Sep 21 '22 05:09

Valentyn Shybanov