Angular Js

Bootstrapping in Angular: How It Works Internally

Bootstrapping in Angular: How It Works Internally

Bootstrapping in Angular: How It Works Internally

Let's learn how Angular functions internally and bootstraps our app in this post on Angular bootstrapping. To start a new Angular project, we used ng new. It produces a large number of boilerplate codes. Additionally, Typescript, Webpack, and Karma are configured. When the app is launched, a straightforward HTML page with various helpful connections to Angular is shown.

Bootstrapping is the process of initializing or loading our Angular application.

Angular takes the following steps to load our first view.
Loads Index.html
Loads Angular & Third-party libraries & Application
Executes application entry point (main.ts)
Load & execute Root Module (app.module.ts)
Executes the Root Component (app.component.ts)
Displayes the template (app.component.html)

Index.html loads first
There are no javascript files in the index.html. Neither there is a stylesheet file. The body of the files has the <app-root></app-root> HTML tag.
Now Build the Application, to view the compiled application, run ng build.

Anular by default create all compiled code inside dist folder.
Now open the dist\getting-started and open the index.html.
You can see that the compiler included four script files. They are runtime, polyfills, styles, & main.

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="icon" type="image/x-icon" href="favicon.ico" />
    <link rel="stylesheet" href="styles.56e3e15f5d5be678.css" />
    <script src="runtime.87dd532f2ee308a5.js" type="module"></script>
    <script src="polyfills.96a2a660c10c5395.js" type="module"></script>
    <script src="main.6ea3cc439cb6e266.js" type="module"></script>

runtime.js: Webpack runtime file
polyfills.js – Polyfill scripts for supporting the variety of the latest modern browsers
styles.js – This file contains the global style rules bundled as a javascript file.
vendor.js – contains the scripts from the Angular core library and any other 3rd party library.
main.js – code of the application.

Loads Angular & Third-party libraries & Application
Both the Angular core libraries and third-party libraries are loaded with the loading of index.html. Now the angular needs to locate the entry point.

Application Entry point - main.ts
The entry point of our application is main.ts. You will find it under the src folder.
The Angular finds out the entry point from the configuration file angular.json. This file is located in the root folder of the project.

"build": {
    "builder": "@angular-devkit/build-angular:browser",
    "options": {
      "outputPath": "dist/getting-started",
      "index": "src/index.html",
      "main": "src/main.ts",              ===>
      "polyfills": [

Before Angular 5, the configuration file was called angular-cli.json. Since the release of Angular 6, it is now angular.json.

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
  .catch(err => console.error(err));


platformBrowserDynamic is the module responsible for loading the Angular application in the desktop browser?
Angular Applications can be bootstrapped in many ways and in many platforms. For example, we can load our application in a Desktop Browser or in a mobile device with Ionic or NativeScript.
If you are using the nativescript, then you will be using platformNativeScriptDynamic it from nativescript-angular/platform library

The AppModule is the app's root module. Modules are how the Angular apps are structured.
Each Angular application must contain at least one module. A root module is the one that loads first when an application is launched.

  .catch(err => console.error(err));

TheplatformBrowserDynamic loads the root module by invoking the bootstrapModule and giving it the reference to our Root module i.e AppModule

Root Module
The angular bootstrapper loads our root module AppModule. The AppModule is located under the folder src/app.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

Root Component
The root module must have at least one root component AppComponent. The root component is loaded when the module is loaded by Angular.

@NgModule class decorator
Use to define an Angular Module and provide metadata about the Modules.
The @NgModule has several metadata properties like imports, Declarations, Providers, Bootstrap

The root component of the AppModule

The Class AppComponent is decorated with @Component Class Decorator.
The @Component class decorator provides the metadata about the class to Angular. It has 3 properties in the above code. Selector, templateURL & styleUrls

The AppComponent defines the template as app.component.html and the CSS Selector is app-root


Suggested Readings
Explain Angular Modules or ngModule

Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.