not really. The difference is really about style. With a service, you get
an instance of it (as a singleton) as a factory, you get whatever the
function returns (as a singleton).

A singleton basically means that there will always only be one (every
accessor references the same instance)

So, to see how it would be the same look at this:

app.service('myThing', function MyThing() {
   this.var = 123;
   this.method = function() {}
});

vs

app.factory('myThing', function() {
   function MyThing() {
       this.var = 123;
       this.method = function() {}
   }
   return  new MyThing();
});


These do basically the same this. the service returns an instance of what
it is automatically, whereas you can return anything from the factory, i
just happen to be returning an instance of a similar class. (note i named
the function MyClass in the service, which isnt needed, but i did it just
to show the similarity)

Therefore, factories are more useful. Eg. If you wanted to return a type,
where you could instantiate many of them, you could do that too:

app.factory('MyThing', function() {
   function MyThing() {
       this.var = 123;
       this.method = function() {}
   }
   return MyThing;
});



On 15 February 2015 at 07:33, Michael Costa <[email protected]> wrote:

> I've never quite understood the word 'singleton'..... ; My understanding
> is that  using a "service" over a "factory" provides the benefit of loading
> data only once per page-refresh and shares it across the application,
> whereas the factory instantiates a fresh call to the data source every-time
> there is a $state change , new controller/scope instance, etc?
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Tony Polinelli

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to