Configuration block
During the load phase of the module, Angularjs configures the module during provider registration and configuration. Throughout the ANGULARJS workflow, this phase is the only part that can be modified before the app is launched.
Angular.module (' myApp ', [])
. config (function ($provide) {});
The most commonly used factory,directive are the syntax sugars for the config function, which are executed during the configuration phase.
It is important to note that Angularjs executes them in the order in which they are written and registered. This means that we cannot inject a provider that is not yet registered. The only exception is the constant () method, which is always executed before all configuration blocks.
Run block
Unlike the configuration block, the run block is executed after the injector is created, which is the first method to be executed in all ANGULARJS applications, closer to the concept of the Main method. Code blocks in a running block are often difficult to unit test, and are highly coupled to the application itself. The run block is typically used to register the global event listener, for example, we set up a listener for routed events in the. Run () block and filter for unauthorized requests.
Suppose we need to execute a function to verify the user's permissions when each route changes, and the only logical place to place this function is the Run method.
Angular.module (' myApp ', [])
. Run (function ($rootScope, authservice{
$rootScope. $on (' $routeChangeStart ', function (evt, next, current) {
If not log in
if (! Authservice.userloggedin ()) {
if (Next.templateurl = = = "Login.html") {
Has turned to login route so no redirection is required
} else {
$location. Path ('/login ');
}
}
})
})
ANGULARJS Module Loading