Adding Run Blocks

After an entire configuration block has finished, the run phase of an AngularJS module can execute. During this phase, you can implement any code necessary to instantiate the module. You cannot implement any provider code during the run block because the entire module should already be configured and registered with the dependency injector by this point.

The run block is a great place to put event handlers that need to be executed at the root level for the application (for example, authentication handlers).

You implement the run block by calling the run() method of the Module object, using the following syntax:

run(function([injectable, . . .]))

A function with the instance injectable parameters is passed in. The injectable parameters should only be instances of injectors because configuration should already have been completed.

The following is a basic implementation of the run block continued from the example:

myModule.run(function(startTime) {
  startTime.setTime((new Date()).getTime());
});

Notice that the startTime instance defined in the config() section shown previously is passed into the run() function. This allows the run() function to update the startTime provider to a new value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset