3

I am confused about something regarding factories and services for Angular.js.

In this question:

angular.service vs angular.factory

It says that services are created with the new keyword.

While in this question:

Confused about Service vs Factory

It says that services are "singletons".

My questions are:

When you add a 'service' as a dependency injection argument to another module, does Angular create a new service each time? Or is it really singleton (as in one will be created, and then provide that specific instance everywhere)?

Community
  • 1
  • 1
corgrath
  • 11,673
  • 15
  • 68
  • 99

2 Answers2

2

Angular services are:

  • Lazily instantiated – Angular only instantiates a service when an application component depends on it.
  • Singletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.

Github official doc.

squiroid
  • 13,809
  • 6
  • 47
  • 67
0

"All AngularJS services are singletons. This means that there is only one instance of a given service per injector."

Read:

John Papa style guide. (In addition to answering your questions, are great recommendations to work with AngularJS).

AngularJS docs.

Doug
  • 195
  • 1
  • 1
  • 13