AngularJS Service
Contents
- What are Services in Angularjs?
- Why use Services in Angularjs?
- Angularjs Service Example
- Different Methods To Create AngularJS Services
- Conclusion
- References
What are Services in Angularjs?
Angularjs Services are functions or objects that contains reusable code that can get consumed accross app by controllers, directives, filters and other services using Dependency Injection Mechanism of Angularjs.
These are also called as singletons, which are the objects that are instantiated only once per application.
Services provide a method for us to keep data around the lifetime of the app and communicate across controllers in a consistent manner.
Why use Services in Angularjs?
Writing your own services is a great way to implement reusable code and they’re the perfect place to capture the logic specific to your application.
Following are its uses:
- To share data between controllers, Services will fetch data from the database, if we store the data inside a controller, once we change to another controller the data will be discarded.This problem can be solved by using $rootscope but, that is not the best way. If we keep the data inside service, that will be accessible for all the controllers.
- It provides a method to keep data across the lifetime of the angular application.
- It is used to organize and share data and functions across the application.
- We can divide our application into multiple different types of components which Angularjs can inject into each other.Modularizing our application makes it easier to reuse, configure and test the components in our application.
Angularjs Service Example
The most simple example I could come up with is alert service or notifier service, consider you are using routing and ng view in your angular app and you want to send classic bootstrap alert box in your main index controller outside of your route controller, than creating a alert service that get consumed by both controller can solve this problem.
Below is the sample code.
Different Methods To Create AngularJS Services
There are five functions you can use to create an Angular service. All of these functions may be called on the built-in provide service. All of them are also exposed on the module object as a convenience.
Angularjs Provider Example
The most fundamental service creation function is the provider function. Creating services with it allows you to create a configurable provider object. The provider knows how to create the resulting service.
The basic process is that the $provide
service creates a provider which contains a function that is used to create a service.
The first function on the $provide service we’ll look at is the provider
function. It’s the most fundamental way to create a service. All of the other methods of creating services(factory, service, and value method) which we’ll learn are just wrappers around the provider function (except constant method).
To use the provider function directly, you simply call the function and pass it a name and a function that will define the underlying provider.
The provider created for each service, will be given a name that is the name you specify for the service with the word provider appended to it. So in the below example, the name of the service we will inject into our other components, will be ‘oauth’, and the name of the provider we will inject in module config method will be oauthProvider to configure our oauth service.
Provider function requires service name as first parameter and second parameter it require is the function which must contain a property named $get.
The return value of the function assigned to $get
property will represent our service that gets injected.
The benefit of using provider method is that it is configurable which at the end decides the resulting service. None of the other service creation functions allow you to do this.
Let’s see how to create and configure a service with the provider function in code below.
In the below example I created an oauth service that can be used for login and logut operation in the app. It internally depends on custom currentUser and formEncode service and angular built-in $http service. You can find currentUser and formEncode service code here and here.
The below three service creation methods all internally call the provider function. They’re just syntactic sugar that make your code a little simpler if you don’t need to provide all of the data required by the provider function.
Factory Method In AngularJs
The factory function is much easier to use than the above provider method. If you don’t need to configure the provider, like we did in the last example, then use the factory function which is much simpler and readable way to create your services.
In the below example I created a simple localStorage service that internally use browser window local storage to store key value data.
Service Method In AngularJs
The next service creation function is Service, which is simply a wrapper around the factory function. When you call the service function, it will internally call the factory function, which will then call the provider function.
Difference Between Service And Factory In Angularjs
The only difference is that the function you passed to the service method will be treated as a constructor function and called with the JavaScript “new” operator. You would use the service method instead of the factory method if you specifically needed your function to be treated as a constructor and called with the “new” operator.
There are several reasons why you may want that behavior. One is if you have defined an inheritance hierarchy in your code. Creating an instance with “new” will make sure that your instantiated object properly inherits from its prototypes. Here is an example,
From the above code you can call logger service output
method which is part of base LoggerBase
class even though logger service is registering CustomerLogger class.
Angularjs Value Service
Similarly, the value function is just a thin wrapper around the factory function. The value function is just shorthand for calling the factory function with no parameters. If you don’t need to inject anything into your factory function, you can use the value function instead.
You use it in much the same way you use the constant service which is the fifth function available for service creation.
Angularjs Constant Service
The constant function is totally different from other services. It allows you to register an object literal function or some other constant value with the injector, but it doesn’t call the service, factory or provider methods behind the scenes. What makes it different from value service is that a value service can’t be injected into a module configuration function, but a constant service can.
AngularJS splits the life-cycle into configuration phase and run phase and you can provide configuration to your application via the config function. Since the config function runs in the configuration phase when no services are available, it doesn’t have access even to simple value service objects created via the Value function method.
This is what the Constant service is for.
Conclusion
There are five methods to create services: Value, Factory, Service, Provider and Constant.
Factory and Service are the most commonly used method. The only difference between them is that the Service method works better for objects that need inheritance hierarchy, while the Factory can produce JavaScript primitives and functions.
The Provider function is the core method and all the other ones are just syntactic sugar on it. You need it only if you are building a reusable piece of code that needs global configuration.
Features / Methods | Factory | Service | Value | Constant | Provider |
---|---|---|---|---|---|
can have dependencies | yes | yes | no | no | yes |
uses type friendly injection | no | yes | yes* | yes* | no |
object available in config phase | no | no | no | yes | yes** |
can create functions | yes | yes | yes | yes | yes |
can create primitives | yes | no | yes | yes | yes |
* at the cost of eager initialization by using new operator directly
** the service object is not available during the config phase, but the provider instance is (see the oauthProvider example above).
References
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox