Introduction
Angular 2 Applications follows the modular structure. Angular 2 Applicatons have one or more modules. Each module has a single dedicated purpose. The module helps us to organize the Application's cohesive group of functionality.
Angular module is in a class with @NgModule decorator function. The @NgModule decorator has metadata, which tells Angular, how to compile and run the module code. Metadata helps to identify the module’s directives, components and pipes. Using metadata, we can make them public, so external components can use them. Angular module exports some function, value and classes from their code.
Angular Modularity provides a great way to organize and extend the Application. Angular 2 has many libraries, which are built as modules like HttpModule, RouterModule etc. The modules can be loaded, when the Application starts and we can make them lazy, loaded asynchronously by the router.
Every Angular Application has at least one module. It is generally referred as "root module". This root module is used to bootstrap the Application. Normally, root module is required in the simple Application. As the Application grows, we need to refactor the root module into the different feature modules, which have related functionality. These feature modules are imported into the root module.
Root Module - AppModule
Every Angular Application has a root module and this class is called “AppModule” by convention.

The decorator @NgModule defines the metadata for the module. Here, we need to import helper module called "BrowserModule". This module needs to be imported by every Browser based Application. It registers the Application Service providers and it also contains the definition of common directives such as NgFor and NgIf. The "declarations" part is a list type and identifies the component of the Applications. In above example, we defined component "AppComponent".
Bootstrapping in main.ts file
The bootstrapping of AppModule is done in main.ts file. Angular offers many bootstrapping options. Here, we consider two options.
Dynamic bootstrapping of Module with the JIT(Just-In-Time) compiler
In this option, Angular compiles the Application in the Browser and after compiling the application successfully, it launches the Application.
Dynamic bootstrapping of module - Main.ts
- //Browser platform with a compiler
- import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
- //Application module
- import { AppModule } from './app.module';
- //Compile and load application module
- platformBrowserDynamic().bootstrapModule(AppModule);
In this option, ahead of time, part of the compiler runs as part of build and generates many class factories. The module "AppModuleNgFactory" is one of them. The bootstrapping done with AppModuleNgFactory is very similar to the dynamic version.
Here, the entire Application was precompiled. Hence, Angular compiler does not need to compile the Application in the Browser. The Application code used in static compilation is much smaller than the dynamic compilation and also it is ready to run immediately.
Static bootstrapping of module - Main.ts
- //Browser platform with a compiler
- import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
- //Application module
- import { AppModule } from './app.module';
- //Compile and load application module
- platformBrowserDynamic().bootstrapModule(AppModule);



Harish PaudelPosted Mar 25, 2019, 7:28 AM
Great sir...
Vignesh ManiPosted Sep 8, 2016, 6:57 PM
Nice one