Powered By Blogger

Tuesday, July 14, 2015

[Angular] Exploring Modules, Dependencies and Global APIs

ng is the core module in angular. This module is loaded by default when an angular app is started. This module provides the essential components for your angular app like directives, services/factories, filters, global APIs and testing components.

How angular modules load the dependencies?

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.

Configuration block – This block is executed during the provider registration and configuration phase. Only providers and constants can be injected into configuration blocks. This block is used to inject module wise configuration settings to prevent accidental instantiation of services before they have been fully configured. This block is created using config() method.

 angular.module('myModule', [])  
 . config(function (injectables) {   
   
 // provider-injector   
 // This is an example of config block.   
 // You can have as many of these as you want.   
 // You can only inject Providers (not instances)   
 // into config blocks.   
   
 }). run(function (injectables) {   
   
 // instance-injector   
 // This is an example of a run block.   
 // You can have as many of these as you want.   
 // You can only inject instances (not Providers)   
 // into run blocks   
   
 });  

Run block – This block is executed after the configuration block. It is used to inject instances and constants. This block is created using run() method. This method is like as main method in C or C++.
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.

When dependent modules of a module are loaded?

A module might have dependencies on other modules. The dependent modules are loaded by angular before the requiring module is loaded.

In other words the configuration blocks of the dependent modules execute before the configuration blocks of the requiring module. The same is true for the run blocks. Each module can only be loaded once, even if multiple other modules require it.

What is Global API?

Global API provides you global functions to perform common JavaScript tasks such as comparing objects, deep copying, iterating through objects, and converting JSON data etc. All global functions can be accessed by using the angular object.

The list of global functions is given below:




No comments:

Post a Comment