AngularJS app.run() 文档? [英] AngularJS app.run() documentation?

查看:24
本文介绍了AngularJS app.run() 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app.run() 如何以及在哪里使用?在模块定义之后,在 app.config() 之后还是在 app.controller()?

How and where is app.run() used? After module definition, after app.config() or after app.controller()?

我正在采用 BreezeJS Angular Q,它要求是否可以在app.run()函数中运行某些代码.

I am adopting the BreezeJS Angular Q, which asks whether certain code can be run in the app.run() function.

推荐答案

调用顺序如下:

  1. app.config()
  2. app.run()
  3. 指令的编译函数(如果它们在dom中)
  4. app.controller()
  5. 指令的链接功能(再次,如果找到)

这是一个简单演示,您可以在其中观看每个演示的执行情况(如果您愿意,还可以进行实验).

Here's a simple demo where you can watch each one executing (and experiment if you'd like).

来自 Angular 的模块文档:

运行块 - 在注入器创建后执行用于启动应用.只有实例和常量可以注入运行块.这是为了防止在应用程序运行时进一步配置系统.

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.

运行块是 Angular 中最接近主方法的东西.一跑块是需要运行以启动应用程序的代码.它在配置完所有服务并且注入器已创建.运行块通常包含的代码是难以进行单元测试,因此应在隔离中声明模块,以便在单元测试中可以忽略它们.

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

使用运行块的一种情况是在身份验证.

One situation where run blocks are used is during authentications.

这篇关于AngularJS app.run() 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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