0

I'm creating a factory service which provides me couple function like

var myApp = angular.module('panelServices', ['ngResource']);

myApp.factory('myService', [...]{ 
  function myService(){
    this.list = [];
  }

  myService.prototype.method1: fn() {} ,
  ...
  myService.prototype.methodn: fn() {},

  return myService;
});

The I Inject myService via DI into my controllers and use new myService() to instanciate a new instance of myService.

I did not find another way to do this, I was thinking of "copying" the service into e.g.: anotherService (based on the fact that service are singletons). My goal is to have a service to use for different models (they do not share data, only methods)-

Please tell me if I did not explain well, thanks in advance.

steo
  • 4,586
  • 2
  • 33
  • 64
  • 1
    inject the model in the service may be ... – Whisher Jan 07 '14 at 16:18
  • do you want multiple instance of your service ? – Tasnim Reza Jan 07 '14 at 16:28
  • practically , yes. I think it is. – steo Jan 07 '14 at 16:41
  • Services operate on models, they shouldn't own it. Maybe you overthink you design. When you call `new` then you essentially don't use automatic dependency injection. So why bother with angular services at all then? If you want to "copy" the service then define `myService` outside the factory and define one service factory for each model. – a better oliver Jan 07 '14 at 19:04
  • @zeroflagL I could agree, but if not in the `controller`, not in a `service`, where do I have to store my model? I think that putting it into a service, insted of putting it symply into the `scope` gives me more separation. Am I thinking it wrong? http://stackoverflow.com/questions/11112608/angularjs-where-to-put-model-data-and-behaviour – steo Jan 07 '14 at 20:50
  • First of all keep in mind that there is no absolute truth :) Second, the accepted answer to that question is about **shared** data, the opposite of what you want. And it doesn't really cover the question, IMHO. I find the answer with the second highest rating much more appropriate. And as for where the model is stored: An angular controller exposes the model to the view through the scope. That's what a controller is there for in the first place. You would use a service to retrieve or manipulate the model. That said, if you are ok with your solution then just use it. – a better oliver Jan 07 '14 at 21:32

1 Answers1

0

For multiple instance of service you can use $injector.instantiate

myApp.factory('myService', function($injector){ 
  function myService(){
    this.list = [];
  }

  myService.prototype.method1: fn() {} ,
  ...
  myService.prototype.methodn: fn() {},

  return function() {
     return $injector.instantiate(myService);
  };
});

And then in controller

myApp.controller('myController', function(myService){ 
      var service = new myService();
//other code
});

in another controller

myApp.controller('anotherController', function(myService){ 
   var service = new myService();
   //other code
});

new myService() it always give you new instance. See this plunker

Tasnim Reza
  • 6,058
  • 3
  • 25
  • 30