AngularJS goes through 2 specific phases during bootstrap:
- configuration phase
- run phase
According to the official documentation:
A module consists of configuration and run blocks that are applied during the application's bootstrapping process. The simplest form of a module contains two types of blocks:
- Configuration blocks - executed during provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks to avoid premature service instantiation.
- Run blocks - run after the injector is created to start the application. Only instances and constants can be injected into run blocks to prevent system reconfiguration during runtime.
The highlighted sentence in the quote above discusses the importance of run blocks:
This is to prevent further system configuration during application run time.
In the AngularJS decorator documentation:
Similar to $provide.decorator
, the module.decorator
function executes
during the app's config phase. This allows you to define a module.decorator before the decorated service itself.
Hence, decorating a controller (or service or filter) occurs in the configuration phase rather than the run phase.
This clarifies why the "asynchronously registering a decorator" example would not work: attempting to define and decorate the controller within the initCtrl
function happens in the run phase, which comes too late for declaring new decorators at this point.