I stumbled across Lukas Ruebbelke excellent book On AngularJS, AngularJS in Action.
When you define a service using a module.service, the instance is returned via a constructor function.This lends itself well to developers who prefer writing Object-oriented code and like to define methods and properties on the this keyword.
angular.module("Mod").
service("myService", function($rootScope){
var service = this;
service.setLoading = function(loading){
// some code goes here
}
});
Now, he show's the same service using module.factory to achieve the same functionality,
angular.module("Mod").
factory("myFactory", function($rootScope){
var setLoading = function(loading){
// some code goes here
};
return {
setLoading : setLoading
}
});
which is very similar to the Revealing Module Design pattern.
The question is not related to the difference b/w service and factory, for that is very much cleared to me, but in which scenarios, one of the design pattern mentioned above preferred over the other and vice-versa.
Or is it just user-preference and better to stick with just one of the two?