28

我想找出AngularJS中的.config和函数之间的区别。.run我正在使用 my.config设置路线,但我确实有一些$on用于观看路线更改开始和成功事件的 's。

然后,我将其中一些代码移到.run.config.

我终于把其中的一些移到了CommonAppController我在<body>.

我也有 2 .config,它似乎运行良好,但这肯定不对吗?

任何人都可以对使用哪种方法有所了解吗?

4

2 回答 2

78

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.

于 2013-08-08T11:25:30.940 回答
-1

.config块在提供者注册和配置阶段执行。这是一个模块级块。

.run块在配置块之后执行。它用于注入服务和常量。

于 2017-09-14T03:45:14.667 回答