我想找出AngularJS中的.config
和函数之间的区别。.run
我正在使用 my.config
设置路线,但我确实有一些$on
用于观看路线更改开始和成功事件的 's。
然后,我将其中一些代码移到.run
了.config
.
我终于把其中的一些移到了CommonAppController
我在<body>
.
我也有 2 .config
,它似乎运行良好,但这肯定不对吗?
任何人都可以对使用哪种方法有所了解吗?
我想找出AngularJS中的.config
和函数之间的区别。.run
我正在使用 my.config
设置路线,但我确实有一些$on
用于观看路线更改开始和成功事件的 's。
然后,我将其中一些代码移到.run
了.config
.
我终于把其中的一些移到了CommonAppController
我在<body>
.
我也有 2 .config
,它似乎运行良好,但这肯定不对吗?
任何人都可以对使用哪种方法有所了解吗?
Configuration blocks and run blocks get executed at different points in the application bootstrap, and have different injection locals at their disposal. Here's a summary of what you can find in the AngularJS documentation.
Configuration blocks (registered with module.config()
) get executed during provider registration, and can only be injected providers and constants (see module.provider()
and module.constant()
). This is typically where you would configure application-wide stuff, such as the $routeProvider
. Stuff that needs to be configured before the services are created.
Run blocks (registered with module.run()
) get executed after the injector has all the providers. Now, all instances and constants can be injected. This is typically where you would configure services, $rootScope
, events and so on.
You can have multiple of either, and they are executed in the order they were registered to the module. Some people prefer to register a configuration block before every group of controllers to register the routes to these controller, for example.
该.config
块在提供者注册和配置阶段执行。这是一个模块级块。
该.run
块在配置块之后执行。它用于注入服务和常量。