Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-Singleton Services in AngularJS

AngularJS clearly states in its documentation that Services are Singletons:

AngularJS services are singletons 

Counterintuitively, module.factory also returns a Singleton instance.

Given that there are plenty of use-cases for non-singleton services, what is the best way to implement the factory method to return instances of a Service, so that each time an ExampleService dependency is declared, it is satisfied by a different instance of ExampleService?

like image 823
Undistraction Avatar asked May 18 '13 15:05

Undistraction


People also ask

What is non singleton service in Angular?

In this case, service is a non-singleton nature. It will create multiple instances of a service. Every time a new instance of provided service will be created when a component is used inside another component. Service is being destroyed along with the component.

Is Angular JS service a singleton?

Lastly, it is important to realize that all Angular services are application singletons. This means that there is only one instance of a given service per injector.

What is singleton in AngularJS?

AngularJS services are: Lazily instantiated – AngularJS 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.

Is AngularJS a factory singleton?

Yes mate, factories and a services are both Singletons.


1 Answers

I'm not entirely sure what use case you are trying to satisfy. But it is possible to have a factory return instances of an object. You should be able to modify this to suit your needs.

var ExampleApplication = angular.module('ExampleApplication', []);   ExampleApplication.factory('InstancedService', function(){      function Instance(name, type){         this.name = name;         this.type = type;     }      return {         Instance: Instance     }  });   ExampleApplication.controller('InstanceController', function($scope, InstancedService){        var instanceA = new InstancedService.Instance('A','string'),            instanceB = new InstancedService.Instance('B','object');             console.log(angular.equals(instanceA, instanceB));  }); 

JsFiddle

Updated

Consider the following request for non-singleton services. In which Brian Ford notes:

The idea that all services are singletons does not stop you from writing singleton factories that can instantiate new objects.

and his example of returning instances from factories:

myApp.factory('myService', function () {   var MyThing = function () {};   MyThing.prototype.foo = function () {};   return {     getInstance: function () {       return new MyThing();     }   }; }); 

I would also argue his example is superior due to the fact that you do not have to use the new keyword in your controller. It is encapsulated within the getInstance method of the service.

like image 131
Jonathan Palumbo Avatar answered Nov 16 '22 01:11

Jonathan Palumbo