If you are a novice in angular and just starting out, then you might be confused by service and factory in angular. Even if you have been using angular in the past, then most probably one of the question peeving you must be the difference between service and factory and when to use them correctly. The forums and groups on angular are also filled with these questions, and although many reply, hardly any reply quenches our need to understand the difference between them. Today I'll try to explain and point out the differences between them.
Since the difference between them is so small and since if I just explain the difference then the blog would end before it fills even the smallest of the screens out there, I will also include the different use cases of factory/service in angular, which would enable us to milk every drop of potential from this very powerful feature of angular.
Lets start with the mandatory coding style or syntax for each first, before getting down the nitty gritty of the difference between them
Why do we return something? Why the variable factory? Lets get to that later, after we see the coding style for a typical service.
Right of the bat, you can see the obvious differences and sorry to dissapoint you but that's all the difference there is technically (if you think about it). Ok let me explain.
In services we can make use of the keyword this. We can use this this because what we define in a service is actually a body of factory instance. So essentially both of them are similar with service just being a convenience method that makes our code a bit shorter and cleaner.
This makes sense because if you check the source for a service in angular its just an instance of a factory:
The factory is said to be much versatile than a service, but you could do everything you could do in a factory using a service, if you declare a service like a factory and it would work just fine (it may break the code in some rare cases but never personally encountered such a situation, at least in most common cases).
Let me clarify something, I do not endorse using services like a factory nor do I myself do so, and this maybe just a hack albeit a very useless one, but just stating the fact.
Now that we have cleared that out, lets see the ways we could use a factory and a service.
The most obvious and widely used use case for a service is to get, post or interact with a rest api.
You could do this using a factory too but you would not be able to use this keyword and would have had to make an object with getEmails as its property (or method) and return it, as in the factory declaration above. But this is what service is essentially for, to reduce lines of code.
So the question arises when to use a factory? Well in all other cases where the return value is not an object, viz, closures, or creating constructors/instances ,etc.
Let me give you examples of both below:
As you can see above, we can create prototype methods in factories and they are registered and also you could create it in any controller. Since the factory is singleton, it would reflect in the whole app, but the person is instantiated with the keyword new everytime so each of them would be different, each time you instantiate. So in other words this factory now returns a class and its properties and methods, which can be changed and accessed from across the app. To register new prototype method in a controller or anywhere outside the factory, we can do the following:
Note :This name_no_space() function will be available anywhere in the app only when the controller(if added in a controller) is run at least once. In usual cases this would mean accessing the page with the controller at least once. This applies if created in any other location (other than a controller), too.
We develop web applications to our customers using python/django/angular.
Contact us at firstname.lastname@example.org