Startuplogic

Startup Logic

Configuration

[Style Y170]
  • Inject code into module configuration that must be configured before running the angular app. Ideal candidates include providers and constants.

    Why?: This makes it easier to have less places for configuration.

  angular
      .module('app')
      .config(configure);

  configure.$inject =
      ['routerHelperProvider', 'exceptionHandlerProvider', 'toastr'];

  function configure (routerHelperProvider, exceptionHandlerProvider, toastr) {
      exceptionHandlerProvider.configure(config.appErrorPrefix);
      configureStateHelper();

      toastr.options.timeOut = 4000;
      toastr.options.positionClass = 'toast-bottom-right';

      ////////////////

      function configureStateHelper() {
          routerHelperProvider.configure({
              docTitle: 'NG-Modular: '
          });
      }
  }

Run Blocks

[Style Y171]
  • Any code that needs to run when an application starts should be declared in a factory, exposed via a function, and injected into the run block.

  • Consider using manual bootstrapping techniques, as an alternative for logic that must run prior to running the Angular app.

    Why?: Code directly in a run block can be difficult to test. Placing in a factory makes it easier to abstract and mock.

    Why?: Code directly in a run block can cause race conditions for startup logic, as it does not have a way to communicate when asynchronous code in the run block has completed.

  angular
      .module('app')
      .run(runBlock);

  runBlock.$inject = ['authenticator', 'translator'];

  function runBlock(authenticator, translator) {
      authenticator.initialize();
      translator.initialize();
  }

Back to Table of Contents