1, Factory ()
The simplest way to create a service inside angular is to use the factory () method.
Factory () Let's define a service by returning an object that contains service methods and data. Within the service method we can inject services such as $http and $q.
In the service, factory () is a great choice when we only need a collection of methods and data and do not need to deal with complex logic.
Note: You cannot use the factory () method when configuring the service with. config ()
2. Service ()
Service () lets us create a service by constructors, and we can define the service using prototype mode instead of the original JavaScript object.
As with the factory () method, we can also see the injection of the service in the definition of the function. The service () method holds the object created by the constructor.
The service () method is well-suited for use in more functional control service
Note: You cannot use the service () method when you need to configure the service using. config ()
3, provider ()
Provider () is the lowest level of service creation, and the only way to configure service creation using the. config () method
Unlike the method mentioned above, we do a dependency injection in the definition of this $get () method.
When to use the provider () method
We need to use the provider () when we want to configure the service before the app starts. For example, we need to configure services to use different backend processing in different deployment environments (development, demo, production).
When we are going to publish open source provider () is also preferred to create a service, so that you can configure services using configuration instead of hard-coding the configuration data into the code.
The difference between Service,factory,provider in Angularjs