admin管理员组

文章数量:1401776

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?

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?

Share Improve this question asked Oct 13, 2013 at 3:56 Kevin BealKevin Beal 10.9k13 gold badges72 silver badges93 bronze badges 2
  • 1 You can find some information on creating your own providers over at this Stack Overflow answer; the same article is available on the Angular.js wiki. – Michelle Tilley Commented Oct 13, 2013 at 4:05
  • 1 Take a look at this answer. It'll probably answer your question aswell. – gustavohenke Commented Oct 13, 2013 at 4:05
Add a ment  | 

1 Answer 1

Reset to default 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 ment): https://github./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.

本文标签: javascriptAngularJSHow can I use config() method with my own servicesStack Overflow