text angularjs config和run的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text angularjs config和run的区别相关的知识,希望对你有一定的参考价值。

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

以上是关于text angularjs config和run的区别的主要内容,如果未能解决你的问题,请参考以下文章

angularjs中的run()方法使用

AngularJS 模块中的run方法

整理了一下angularJs的webpack模板

AngularJs 模块的创建

angularjs1-4 事件指令

angularjs怎么发异步请求