Can $ inject be used for services in AngularJS, or is this only necessary for controllers?

I know that for minimization and obfuscation purposes, we should always use the $ injector (via controllerName.$inject = ['$service', '$service2'] ) to indicate the actual names of the services that are required.

If I write a custom service that relies on other services, can I do the same? The only examples I can find to use the method. $ Injection, called on controllers.

If i do

  myModule.factory('myService', function($rootScope, anotherService) { return { foo: 'bar' }); 

Should I add this?

myService.$inject = ['$rootScope', 'anotherService'];

Or perhaps this applies to the module as a whole?

myModule.$inject = ['$rootScope', 'anotherService'];

... But perhaps in this case the module is already tracking its services, and therefore minimization is not a concern?

+4
source share
1 answer

Check out the Dependency Injection Guide , Built-in Annotation .

The following is also valid syntax, and it is safe to minimize:

 myModule.factory('myService', ['$rootScope', 'anotherService', function($rootScope, anotherService) { .... }]); 
+4
source

All Articles