User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper Importance of Config And Run Blocks in AngularJS

Hi friends, today we will discuss some important points of AngularJS. There are two important blocks in AngularJS, which is not popular but we mostly use in our Angular Application. Both are used when we defined our ng-app with angular.module in our javascript file.

Today we will discuss about the importance of config() and run() block in AngularJS application. So, now move ahead and start discussion in sequence and first take config() block.

In Angular application most of objects are instantiated and wired together automatically by the injector service. We can use five types of recipe with injector or we can say injector identify only five type of recipe to create any objectThis block is used to prevent accidental instantiation of service before they have been fully configured.

We can use config() block to inject only providers and constants in our AngularJS application. This block executed in AngularJS application during the provider registrations and configuration phase. Now we have several types of providers in our AngularJS application. But if we want to use these provider first we need to inject these providers in our application with the help of config() block.

The providers basically create new instances, but only once for each provider.

Look at the following provider, which we can use in our application:

1.) $httpProvider
2.) $locationProvider
3.) $routeProvider
4.) $provider
5.) User defined Provider
6.) Constant

In the above discussion we know the importance of config() block and provider but here we have an important question- "how we use config() block in our application?"

Look at the following code:

  1. angular.module('crazydeveloperModule', [])
  2. .config(['$httpProvider','$locationProvider','$routeProvider','$provider','cookieProvider','constant',
  3. function ($httpProvider,$locationProvider,$routeProvider,$provider,cookieProvider,constant) {
  4. // provider-injector
  5. // This is an example of config block.
  6. // You can have as many of these as you want.
  7. // You can only inject Providers (not instances)
  8. // into config blocks.
  9. }])

In the above code we can see the way we can use config() block in our application. Now move ahead to next point, here we have run() block. We know that in config() block can't inject any instances. run() executed after the config() block and here we have facility with this block to inject any instance and constants in our application. This block is just like main() method in other language. This block is a great place to put any event handlers that we need to be executed at the root level for the application. For example, authentication handlers.

Again we have an important question- "how we use run() block in our application?"

Look at the following code:

  1. angular.module('myModule', [])
  2. . run(['$rootscope','security','pageFactory','$httpBackend',
  3. function ($rootscope,security,pageFactory,$httpBackend) {
  4. // instance-injector
  5. // This is an example of a run block.
  6. // You can have as many of these as you want.
  7. // You can only inject instances (not Providers)
  8. // into run blocks
  9. }]);

After all of the above discussion we can say "An Angular Module use Configuration and Run blocks to inject dependencies (like providers, services and constants) which get applied to the angular app during the bootstrap process."

I hope now we understand the important of the above block in our AngularJs application.

Happy reading!!!
Abhishek Kumar

crazydeveloper Home Page 24 May 2015

Become a Fan