.config, .run, AppCtrl - 在哪里放置路由? [英] .config, .run, AppCtrl - where to put routes?

查看:22
本文介绍了.config, .run, AppCtrl - 在哪里放置路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to find out the difference between the .config and .run functions in AngularJS. I was using my .config for setting up routes, but I did have some $on's for watching route change start and success events.

然后我将部分代码移至 .run,因为我在 .config 中遇到了一些依赖注入问题.

I then moved some of this code to .run as I was having some dependency injection problems in .config.

我终于将其中的一些移到了一个 CommonAppController,我已经在我的 上设置了它.

I finally moved some of this to a CommonAppController which I have set on my <body>.

我也有 2 个 .config 并且它似乎运行正常,但这肯定不对吗?

I also had 2 .config's and it seemed to be running ok, but surely this isn't right?

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

Can anyone give a little insight on which method to use?

推荐答案

配置块和运行块在应用程序引导程序中的不同点执行,并有不同的注入局部变量可供他们使用.以下是您可以在 AngularJS 文档中找到的内容摘要.

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.

配置块(使用 module.config() 注册)在提供程序注册期间执行,并且只能注入提供程序和常量(参见 module.provider()module.constant()).这通常是您配置应用程序范围的东西的地方,例如 $routeProvider.在创建服务之前需要配置的东西.

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.

运行块(注册到module.run())在注入器拥有所有提供者后被执行.现在,可以注入所有实例和常量.这通常是您配置服务、$rootScope、事件等的地方.

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, AppCtrl - 在哪里放置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆