1

I'm learning AngularJS and I'm seeing the term "service" used in three different places in the developer guide, and it's confusing me just a little bit.

A. In the Angular developer guide, there's a section on services. It goes on to describe how to declare these using myModule.factory.

B. In the Angular developer guide, there's a section on providers, which says that the injector service "creates two types of objects, services and specialized objects." It gos on to describe the 5 recipes for services, one of which is:

C. The service recipe.

So it seems like A is an entire section redundantly covering C, which is already part of B, which describes a service as being one possible type of service.

I am confused. Why is a Service a type of service? Is this just poorly chosen terminology combined with redundant docs? Why does the Service section (A) cover creating services with angular.factory, which is one of the other 5 "types of service", alongside Service?

TylerH
  • 20,799
  • 66
  • 75
  • 101
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • possible duplicate of [Service vs provider vs factory?](http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory) – squiroid Mar 07 '15 at 07:50
  • Already read that multiple times, does not in any way address my specific question. I am not confused on the differences between the 5 types of provider recipes. I am confused between the 3 ways the developer guide is using the term `service`. – temporary_user_name Mar 07 '15 at 07:59
  • Sorry, I see now that I may have misunderstood your question. I do agree that it's strange that it's both a section on services and providers in the docs. In my mind, there should only exist a section on services. That section should in turn describe providers as the main recipe, and in turn all the other service "sub"-recipes. – Kjell Ivar Mar 08 '15 at 19:53

2 Answers2

1

Short answer: Poor choice of terminology, which creates confusion. From the official docs on providers:

Note: Yes, we have called one of our service recipes 'Service'. We regret this and know that we'll be somehow punished for our misdeed. It's like we named one of our offspring 'Child'. Boy, that would mess with the teachers.

There are not 5 service types. In reality you just have one type: Providers. They let you provide a service. All the other recipes are built on top of this, and exist only to make your life simpler (syntactic sugar). For example, if all you want to do is to set one value and to read it, then the base "provider" recipe will feel a bit verbose. That's why the creators of Angular let you use .value instead.

And just to show, how the "Service recipes" are just variations of basically the same thing, here is a service written with both .factory and .service recipes. They should exhibit exactly the same behaviour.

angular.module('myModule').factory('MyService', function(){
    var service = {};

    service.myFunc = function(){
        console.log('Hello there');
    };

    return service;
});

angular.module('myModule').service('MyService', function(){

    this.myFunc = function(){
        console.log('Hello there');
    };
});

I find that what recipe you use often comes down to either:

  • What you prefer (coding style, this vs object for example)
  • You need to be able to config your service in the config phase of your module
Kjell Ivar
  • 1,154
  • 8
  • 8
0

I agree that it's confusing, because we as developers overuse the term service. Here's my attempt to help you.

  • There are singleton JavaScript objects that are created by the AngularJS injector via registered recipes. These are services in senses A and B in your question. The recipe used to register them (provider, factory, or service) is really irrelevant to the object that you get from the injector.
  • One kind of singleton JavaScript object is created by the AngularJS injector by simply instantiating an existing JavaScript type. This kind of object is registered via the service recipe. Why this is called the service recipe, and not the existingType recipe or something, is beyond me.
John Bledsoe
  • 17,142
  • 5
  • 42
  • 59