3

I recently used the .config() method on an angular module I was developing in order to use AngularJS's routes. It's looked something like:

myModule.config([
    '$locationProvider',
    '$routeProvider',
    function ($locationProvider, $routeProvider) {

    $locationProvider.html5Mode(false);
    $locationProvider.hashPrefix('!');

    $routeProvider.when('/', {
        controller: 'myCtrl'
    });
}]);

How does this method work? Can I configure my own services using this method? Should I?

Kevin Beal
  • 10,500
  • 12
  • 66
  • 92
  • 1
    You can find some information on creating your own providers over at [this Stack Overflow answer](http://stackoverflow.com/questions/16828287/what-things-can-be-injected-into-others-in-angular-js/16829270#16829270); the same article is available [on the Angular.js wiki](https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection). – Michelle Tilley Oct 13 '13 at 04:05
  • 1
    Take a look at [this answer](http://stackoverflow.com/a/15666049/2083599). It'll probably answer your question aswell. – gustavohenke Oct 13 '13 at 04:05

1 Answers1

4

During the config phase, only providers can be injected. So I think that you can create a custom provider and then configure it during the config phase.

See this documentation (already mentioned by Brandon Tilley in a comment): https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection#configuring-providers

Basically angularjs first invoke the config method and then invoke the run method. During config only providers are available. A provider can then be used to create service instance.

Davide Icardi
  • 11,919
  • 8
  • 56
  • 77
  • I may be incorrect, but aren't constants also available in the config phase? https://docs.angularjs.org/guide/providers#conclusion – hyang123 Jul 02 '16 at 15:11