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

我采用的是BreezeJS Angular Q,它会询问某些代码是否可以在app.run()函数中运行。


当前回答

特别是……

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

地点:

例如/packages/dashboard/public/controllers/dashboard.js

How:

让它看起来像这样

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'
});

其他回答

下面是调用顺序:

app.config () app.run () 指令的编译函数(如果它们在dom中找到) app.controller () 指令的链接函数(如果找到的话)

这里有一个简单的演示,您可以观看每个程序的执行(如果您愿意,还可以进行实验)。

来自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. 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.

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

特别是……

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

地点:

例如/packages/dashboard/public/controllers/dashboard.js

How:

让它看起来像这样

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'
});