In most examples I find of using Services in angularJs I find that the service is really a factory. eg
angular.module('myModule').factory('customerService', [
'$http',
function ($http) {
return {
get: function () {
return doSomeStuff();
}
};
}
]);
The author has created a factory but called it customerService instead of customerFactory. Why is this? Is there some stigma against factories or are angular services so super-cool that everyone wants kudos for writing them, even when they don't.
Also, to be clear to anyone answering this, I'm not asking for the comparative merits of factories or services I'm asking why the angular community is deliberately misnaming their objects.
Factory is the most basic provider that you can specify external dependencies while constructing your services. This is what Angularjs documentation says about naming them.
https://docs.angularjs.org/guide/providers
Best Practice: name the factory functions as Factory (e.g., apiTokenFactory). While this naming convention is not required, it helps when navigating the codebase or looking at stack traces in the debugger.
So it is a developer's choice but surely not the recommended way.
Although the code you posted does indeed register a service factory, the first parameter of $provide.factory() is the name of the service instance (services are singletons). This name is used by the $injector to determine which factory is called when you declare a dependency on the service. See more here:
https://docs.angularjs.org/api/auto/service/$provide
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With