Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS app.run() documentation?

Tags:

angularjs

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

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

like image 363
user3071284 Avatar asked Dec 18 '13 16:12

user3071284


People also ask

What is app run in AngularJS?

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.

What is difference between config () and run () method in AngularJS?

config block is executed during the provider registration and configuration phase. It' a module level block. The . run block is executed after the config block.

What is rootScope in AngularJS?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.

What must be completed to run AngularJS in production?

By default AngularJS compiles and executes all directives inside comments and element classes. In order to perform this task, the AngularJS compiler must look for directives by: Parse all your application element classes. Parse all your application html comments.


2 Answers

Here's the calling order:

  1. app.config()
  2. app.run()
  3. directive's compile functions (if they are found in the dom)
  4. app.controller()
  5. 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:

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.

One situation where run blocks are used is during authentications.

like image 191
KayakDave Avatar answered Oct 17 '22 17:10

KayakDave


Specifically...

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

Where:

In your package.js E.g. /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'
});
like image 45
Michael Cole Avatar answered Oct 17 '22 17:10

Michael Cole