Reputation: 55
I have a service called tableService created using the code as follows:
app.factory('tableService', [function() {
var data = some data;
var foo = function (){
//do something to data here
};
return {
data: data,
foo: foo
}
}]);
I want to create another service that has the same properties as tableService so that I can use its functionality on a different set of data.
Is there anyway I can do something like
app.factory('tableService', 'secondService', [function() {
}]);
So that I don't have to repeat the code twice, one for each service?
Upvotes: 2
Views: 192
Reputation: 1361
You can use, a local object definition, create new instance from it by using a getInstance
method.
app.factory('tableService', [function() {
var data = some data;
MyService(){
// code ...
this.data = data;
}
MyService.prototype.foo = function(){
// code ...
}
function getInstance(){
return new MyService();
}
return {
getInstance: getInstance,
}
}]);
Creating instances :
var tableSericeA = tableService.getInstance();
var tableSericeB = tableService.getInstance();
Upvotes: 4