0

I have been using AngularJS on a new project and I really like it. So much so I think I will do all my future web projects with it. I have done lots of reading on the different types of Provider there are in Angular and have used Factories and Values a lot.

I understand the difference between Services and Factories in as far as a Service is an instance of the function you pass to module.service() and a Factory is the return value of the function you pass to module.factory() (often an object literal in my case). I can not really see why you would need to use a Service though.

The functionality of a Service seems identical to returning an object literal with properties and methods from the factory function. For example:

module.service('MyService', function () {
    this.value = 'Hello';

    this.sayValue = function () {
        alert(this.value);
    };
});

module.factory('MyFactory', function () {
    // Can also do some things before return statement
    return {
        value: 'Hello',
        sayValue: function () {
            alert(this.value);
        }
    };
});

The factory has the added functionality of being able to perform some actions before the return statement is encountered.

Can anyone explain where they draw the line between Service and Factory usage?

Thanks in advance

Ben Guest
  • 1,488
  • 2
  • 17
  • 29
  • Possible duplicate of [this](http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory) or [this](http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory) – Alireza Ahmadi Aug 08 '14 at 22:57
  • I have read that article, I know what the difference is, but just can't see why you would use a service over a factory. I'm looking for a use case for service that factory would not be suitable for – Ben Guest Aug 08 '14 at 22:59
  • I've kinda gone in the opposite direction. I tend to use services far more often than I use factories. Whatever floats your boat. – Kevin B Aug 08 '14 at 23:01
  • 1
    This should work for you : http://stackoverflow.com/questions/18939709/angularjs-when-to-use-service-instead-of-factory – Alireza Ahmadi Aug 08 '14 at 23:04
  • Okay, so basically they kind of satisfy the same need, and it's whatever you prefer? – Ben Guest Aug 08 '14 at 23:04
  • 1
    I think this is a good question but a bit opinion based. I think more help could be found in programmers.stackexchange.com. there you can find good advises/oppinions on various topics – DaGhostman Dimitrov Aug 08 '14 at 23:06

1 Answers1

0

As shown in the docs at https://docs.angularjs.org/guide/providers#service-recipe , if you want to do the following:

myApp.factory('unicornLauncher', ["apiToken", function(apiToken) {
  return new UnicornLauncher(apiToken);
}]);

Then you can just do:

myApp.service('unicornLauncher', ["apiToken", UnicornLauncher]);

The docs say this is

Much simpler!

But I have to say, in my opinion, I don't think it's clear, and the saving of a line of code isn't worth it in this case. For both clarity and flexibility, I now always use a factory.

Michal Charemza
  • 25,940
  • 14
  • 98
  • 165
  • I agree. I also think that `UnicornLauncher` could be an object literal. I don't see the advantage of creating a `new UnicornLauncher` when you consider that all Angular providers are singletons anyway. – Ben Guest Aug 09 '14 at 10:58