本文翻译自:AngularJS app.run() documentation?
How and where is app.run()
used? app.run()
使用方式和位置在哪里? After module definition, after app.config()
or after app.controller()
? 模块定义之后,在app.config()
之后或在app.controller()
?
I am adopting the BreezeJS Angular Q , which asks whether certain code can be run in the app.run()
function. 我正在采用BreezeJS Angular Q ,它询问某些代码是否可以在app.run()
函数中运行。
#1楼
参考:https://stackoom.com/question/1OhPQ/AngularJS-app-run-文档
#2楼
Here's the calling order: 这是调用顺序:
-
app.config()
-
app.run()
- directive's compile functions (if they are found in the dom) 指令的编译函数(如果它们在dom中找到)
-
app.controller()
- directive's link functions (again, if found) 指令的链接功能(再次,如果找到)
Here's a simple demo where you can watch each one executing (and experiment if you'd like). 这是一个简单的演示 ,您可以在其中观看每个执行(如果您愿意,可以进行实验)。
From Angular's module docs : 来自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. 这是为了防止在应用程序运行时进一步进行系统配置。
Run blocks are the closest thing in Angular to the main method. 运行块是Angular中与main方法最接近的东西。 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 . 使用运行块的一种情况是在认证期间。
#3楼
Specifically... 特别...
How and where is
app.run()
used?app.run()
使用方式和位置在哪里? After module definition or afterapp.config()
, afterapp.controller()
? 在模块定义之后或在app.config()
之后,在app.controller()
?
Where: 哪里:
In your package.js Eg /packages/dashboard/public/controllers/dashboard.js
在你的package.js例如/packages/dashboard/public/controllers/dashboard.js
How: 怎么样:
Make it look like this 让它看起来像这样
var app = angular.module('mean.dashboard', ['ui.bootstrap']);
app.controller('DashboardController', ['$scope', 'Global', 'Dashboard',
function($scope, Global, Dashboard) {
$scope.global = Global;
$scope.package = {
name: 'dashboard'
};
// ...
}
]);
app.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
});