Lansana Camara
Lansana Camara

Reputation: 9873

What runs first in an AngularJS app? (module, config, run, etc.)

I am curious about the way I am structuring my app (I'm probably wasting time on things that don't matter), but curiosity leads me to ask nonetheless.

In AngularJS, how does an app work? For example, if I have this code in app.js (removed my implementations, just showing the structure):

(function() {

    'use strict';

    angular
        .module('app', ['ui.router', 'satellizer', 'permission', 'angular-jwt', 'ui.bootstrap', 'angular-loading-bar'])
        .config(['$stateProvider', '$urlRouterProvider', '$authProvider', '$locationProvider', '$httpProvider', Config])
        .run(['$rootScope', '$state', '$auth', 'jwtHelper', 'Permission', Run]);

    function Run($rootScope, $state, $auth, jwtHelper, userService, Permission) {
        // Some code
    }

    function Config($stateProvider, $urlRouterProvider, $authProvider, $locationProvider, $httpProvider) {
        // Some code
    }

})();

.. what runs first? I'm curious because I want to know if I should place run before config, and so on. I don't want config to load but be waiting on run to do something, when it would be more efficient to have run first so there is no waiting..if that's even a problem to begin with.

Any clarification would be appreciated.

Upvotes: 3

Views: 11288

Answers (3)

bhavik4748
bhavik4748

Reputation: 71

When bootstrapping, first AngularJS applies all constant definitions. Then AngularJS applies configuration blocks in the same order they were registered.

src: https://docs.angularjs.org/guide/module#!%2F


Order Of Call

  1. app.config()
  2. app.run()
  3. Directive's compile functions if they are found in the DOM.
  4. app.controller()

src:https://gxbsst.gitbooks.io/angularjs-foundations/config_vs_run.html

Upvotes: 6

Chris Halcrow
Chris Halcrow

Reputation: 31960

From the official Angular documentation (https://docs.angularjs.org/guide/module#!/)

"Run Blocks

Run blocks are the closest thing in AngularJS 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."

(the 'config' is configuring services that are being instantiated using the 'provider' method)

Upvotes: 4

kumar6027
kumar6027

Reputation: 21

First it will config and then run will be executed

Upvotes: 2

Related Questions