Introduction
Every module consist of name and dependencies on zero or more modules, a "config" method, and a "run" method. Config and run blocks are executed as soon as the module containing the block is loaded into the memory.
Config & Run Execution Sequence
AngularJs orders all modules in dependency chain according to their dependencies, and then first executes the
In the above example execution sequence of run and config is as described below.
1) In dependent config block.
2) In dependent second config block.
3) In main config block.
4) In dependent run block.
5) In dependent second run block.
6) In main run block.
From the above example it is clear that first inner dependent module code blocks execute and then main module code block execute.
Let us see diagrammatic representation of the same.
Config Code Block
Config code block get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Source: angular.org
In config blocks we can configure our providers to get more control over services, we can look later on about the providers when we dive into the services also in routing section we will see how they are implemented in angular routing.
See below the code snippet how config works with provider
Run Code Block
Run block 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 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.
Source: angular.org
Every module consist of name and dependencies on zero or more modules, a "config" method, and a "run" method. Config and run blocks are executed as soon as the module containing the block is loaded into the memory.
Config & Run Execution Sequence
AngularJs orders all modules in dependency chain according to their dependencies, and then first executes the
config
method of each module in order of their dependency [execute for first dependency then second and so on, and finally the main module blocks], and then executes the run
blocks of each module in the same orders.
In the above example execution sequence of run and config is as described below.
1) In dependent config block.
2) In dependent second config block.
3) In main config block.
4) In dependent run block.
5) In dependent second run block.
6) In main run block.
From the above example it is clear that first inner dependent module code blocks execute and then main module code block execute.
Let us see diagrammatic representation of the same.
Config Code Block
Config code block get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Source: angular.org
In config blocks we can configure our providers to get more control over services, we can look later on about the providers when we dive into the services also in routing section we will see how they are implemented in angular routing.
See below the code snippet how config works with provider
Run Code Block
Run block 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 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.
Source: angular.org