In AngularJS, why does the convention call the factory service

In most examples, I find 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 created a factory, but named it customerService instead of customerFactory. Why is this? Is there any kind of stigma about the factories or the angular services are so super-cool that everyone wants to get recognition for them, even if they don’t.

Also, to be understood by anyone who answers this, I am not asking for the comparative merits of factories or services, which I ask why the angular community is deliberately misusing its objects.

+5
source share
2 answers

Although the code you submitted did indeed register the factory service, the first parameter of $ provision.factory () is the name of the service instance (the services are single). This name is used by the $ injector to determine which factory call to call when declaring a service dependency. See here for more details:

https://docs.angularjs.org/api/auto/service/$provide

+1
source

Factory is the easiest provider that can specify external dependencies when building your services. This is what the Angularjs documentation says about their name.

https://docs.angularjs.org/guide/providers

Best Practice: name the factory functions as factory (e.g. apiTokenFactory). Although this naming convention is not required, it helps when navigating the codebase or finding stack traces in the debugger.

So, this is the choice of the developer, but certainly not recommended.

+2
source

Source: https://habr.com/ru/post/1203740/


All Articles