1

When an angular controller needs to invoke some logic, like perhaps calling a web API, that logic can be encapsulated in an angular service or factory. (There are other options, but that's not important for the difference between a service and a factory.)

I just did some googling and there are a ton of articles and videos out there that explain what services and factories are. These articles and videos don't explain which one to use and why. So if I'm writing code in my angular controller and I want to use a factory or service, which one should I use and why?

I don't care about how they work, that at least one of them is a singleton, that one returns and instance of a function, one is used with the new keyword, blah...

Both services and factories appear to be used and called in the same way. So can someone explain in which case I would use one over the other and why? Does it even matter?

** Edit **

The suggested possible answer states this:

You can accomplish the same thing with both. However, in some cases the factory gives you a little bit more flexibility to create an injectable with a simpler syntax. That's because while myInjectedService must always be an object, myInjectedFactory can be an object, a function reference, or any value at all.

If that's it, then my question is indeed answered. But it would be nice to know if that's the case. Is that the only reason to use a factory over a service?

Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • 4
    There are some good writeups at http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory/15914263 – Joe Pontani Aug 07 '15 at 16:05
  • The answer lies in all the stuff that you don't care about (how they work, etc). In the end, they are both producing the same thing: a singleton object, that you can inject into other application components. Since they are producing the same result, it doesn't matter which one you use. – Sunil D. Aug 07 '15 at 16:13
  • @JoePontani The problem I've found with all of these articles is that there is a lot of explanation about the difference between the two *internally*. Most of them don't talk about which one to use from the caller's perspective. I thought it would be nice to have one location where someone cuts through all of the details and just says: "Use this in this case. Here is why..." – Bob Horn Aug 07 '15 at 16:27
  • The issue is that this question asks the same thing as the other question. It even has the text "Which should be used for what (assuming they do different things)?". – George Stocker Aug 07 '15 at 17:19

1 Answers1

1

Use factory if object definition syntax is preferable

app.factory('factory', function () {
    ...
    return {
        value: '...'
    };
});

or it returns something that is not an object for some reason.

Use service if this syntax is preferable

app.service('service', function () {
    this.value = '...';
});

or it should return an object created with new from another constructor, e.g.

app.factory('factory', function () {
    return new someConstructor();
});

vs.

app.service('service', someConstructor);

A good use case for service is that you can seamlessly refactor existing controllers with controllerAs syntax to inherit from common service, in this case no this statements replacement is required, as shown here:

app.service('parentControllerService', function () {
    this.value = '...';
});

app.controller('MainController', function (parentControllerService) {
    angular.extend(this, parentControllerService);
});

app.controller('ChildController', function (parentControllerService) {
    angular.extend(this, parentControllerService);
    this.value = '!!!';
});
Estus Flask
  • 206,104
  • 70
  • 425
  • 565