首页 > angularjs中config和run的区别?

angularjs中config和run的区别?

这两者都是可以配置东西的吧,有什么相同的和区别?


补充一下 @Showonne 的答案,执行顺序不同,注入服务类型不同。补充一个清单:

最近,在项目中遇到需要在 config 阶段中注入一些service的情况,然而 factory,service 还有 value 是不能在 config 中注入的,先看一个清单:

服务/阶段 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/ShuyunXIAN...


我提过一个相同的问题,链接
区别不算大,执行顺序不同,再有就是允许注入的东西不同,config里允许注入的是Providerconstance(常量)run里允许注入的是实例和constance

搬运官方文档相关介绍

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.

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.

【热门文章】
【热门文章】