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.Useful for service configuration.
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.Useful for application initialization.
Useful for application initialization.
So you can't inject your own service, or built-in services like $http into config(). Use run() instead.
所以下面的代码会报错
app.run(['$httpProvider','httpService', function ($httpProvider,httpService) {
$httpProvider.interceptors.push(function() {
})
}])
报错:Unknown provider: $httpProviderProvider
app.config(['$httpProvider','httpService', function ($httpProvider,httpService) {
httpService.setWebViewData();
}])
报错:Unknown provider: httpService
参考:https://code.angularjs.org/1.3.0-beta.14/docs/guide/module
https://code.angularjs.org/1.2.30/docs/api/ng/type/angular.Module
服务/阶段 | provider | factory | service | value | constant
-------- | -------- | ------- | ------- | ----- | --------
config阶段 | Yes | No | No | No | Yes
run 阶段 | Yes | Yes | Yes | Yes | Yes
> 注意,provider 在config阶段,注入的时候需要加上 provider 后缀,可以调用非 $get 返回的方法
> 在 run 阶段注入的时候,无需加 provider 后缀,只能调用 $get 返回的方法
参考:https://github.com/ShuyunXIANFESchool/FE-problem-collection/issues/21