javascript - How to inject 2 different instances of a module in AngularJs -


say have crudservice.js file:

crudservice.module('genericcrudservice', ['$http', '$log', 'config',  function ($http, $log, config) {         ....         return {             setsomeattribute: function(m) {                 // set attribute             }         }     }]); 

and have module need differently configured instances of crud service:

module.factory('task', ['genericcrudservice','genericcrudservice', function (service, actionservice) {         ...         return {            init: function(p) {                service.setsomeattribute('a');                actionservice.setsomeattribute('b');            }         }     }]); 

but noticed when trying use service variable, attribute set 'a'. doing wrong?

angular services are singletons cached on first injection:

all services in angular singletons. means injector uses each recipe @ once create object. injector caches reference future needs.

genericcrudservice refers same service, injecting service , actionservice variables results in having instance of genericcrudservice service in both variables, service === actionservice.

if service should new instance in cases, service instance may method (e.g. factory or getinstance) returns new instance of object:

  app.factory('service', () => {     function service() {       this.method = ...;       this.factory = () => new service;     }      return new service;   });    ...   var serviceinstance = service.factory(); 

if service should new instance everywhere, should return factory or constructor function instantiated manually:

  app.factory('service', () => function () {     this.method = ...;          });    ...   var serviceinstance = new service; 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -