4

I'm aware that for the purposes of minification and obfuscation we should always use the $injector (by way of controllerName.$inject = ['$service', '$service2']) to specify the actual service names that are required.

If I write a custom service that relies on other services, however, can/should I do the same thing? The only examples I can find for using the .$inject method are called on Controllers.

If I am doing

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

Should I append this?

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

Or perhaps it's applied to the module as a whole then?

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

...But maybe in that case, the module is already keeping track of its services, and hence minification is not a concern?

4

1 回答 1

4

Check the dependency injection guide, section Inline Annotation.

The following is also a valid syntax, and it is safe for minification:

myModule.factory('myService', ['$rootScope', 'anotherService', 
      function($rootScope, anotherService) {

        ....
}]);
于 2013-03-30T19:33:42.120 回答