How to create (singleton) AngularJS services in 4 different ways
Next to creating controllers and directives, AngularJS also supports “singleton” services. Services, like on the server-side, offer a great way for separating logic from your controllers. In AngularJS anything that’s either a primitive type, function or object can be a service. Although the concept of service is quite straight forward, the declaration of them in AngularJS isn’t:
- There are 4 different ways to declare a service.
- Registering a existing value as a service
- Registering a factory function to create the singleton service instance
- Registering a constructor function to create the singleton service instance
- Registering a service factory which can be configured
- Only 1 of them is extensively documented