Why can't I get a $location injected into in my config()?

后端 未结 2 400
既然无缘
既然无缘 2021-02-05 04:35

Why does this give me an error:

angular.module(\'app\')
       .config(function($routeProvider, $locationProvider, $httpProvider, $location) {
2条回答
  •  生来不讨喜
    2021-02-05 05:05

    Only providers and constants may be injected into configuration blocks.

    From the angularjs documentation on configuration blocks

    1. Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured

    2. Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

    Essentially the configuration block is where you configure providers before they are injected into controller, services, factories and so on.

    angular.module('myModule', []).
     config(function(injectables) { // provider-injector
       // This is an example of config block.
       // You can have as many of these as you want.
       // You can only inject Providers (not instances)
       // into the config blocks.
     }).
     run(function(injectables) { // instance-injector
       // This is an example of a run block.
       // You can have as many of these as you want.
       // You can only inject instances (not Providers)
       // into the run blocks
     });
    

提交回复
热议问题