Sunday, November 7, 2021

Angular Application Bootstrap Process

When you create an Angular project using Angular CLI ng new command, the CLI installs many npm packages resulting in generation of lot of files. If you are overwhelmed with the number of files and looking for the flow of the Angular application then this article may help you in understanding that. Here we’ll see how Angular bootstraps an application.

Angular application bootstrapping process

When you build your application using ng serve command which builds and serves your app, flow of the Angular is as follows-

  1. First file that is referenced is angular.json which gives information about:
    • where to find the source of the application.
    • How to build the application.
    • Which files to refer for testing.
    • Where to find the main file.
  2. Main file which happens to be main.ts provides information about which module to bootstrap.
    import { enableProdMode } from '@angular/core';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    
    import { AppModule } from './app/app.module';
    import { environment } from './environments/environment';
    
    if (environment.production) {
      enableProdMode();
    }
    
    platformBrowserDynamic().bootstrapModule(AppModule)
      .catch(err => console.error(err));
     

    Here platformBrowserDynamic is the module, which is responsible for loading the Angular application in the desktop browser.

    import { AppModule } from './app/app.module'; tells where to find the AppModule class.

  3. Using this module information ng searches for the AppModule class which is specified in the src/app/app.module.ts file.
    @NgModule({
      declarations: [
        AppComponent,
        HelloWorldComponent
      ],
      imports: [
        BrowserModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    
  4. From the app.module.ts file ng gets the information about the components and which component is to be bootstrapped as the top level component. As per the AppModule referred here the component that has to be bootstrapped is AppComponent which is specified in src/app/app.component.ts file.
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
     ...
     ...
    }
    
  5. Component specifies the selector, template and CSS that has to used. As you can see selector property here specifies the app-root (same as used in index.html). Which means any <app-root></app-root> tags that are used within a template will be compiled using the AppComponent class and get the functionality as defined in the class.
  6. With in the angular.json index.html is defined as the first page to be loaded. In index.html you will find the same <app-root></app-root> tags.
    <body>
      <app-root></app-root>
    </body>
    

    Template specified in AppComponent ( app.component.html) is rendered between these tags using the flow as explained above.

That's all for this topic Angular Application Bootstrap Process. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Angular Tutorial Page


Related Topics

  1. Angular Project Structure With File Description
  2. How to Install Node.js and NPM in Windows
  3. How to Setup Angular
  4. Creating New Component in Angular
  5. How to Add Bootstrap to Angular Application

You may also like-

  1. Angular ngFor Directive With Examples
  2. Angular Event Binding With Examples
  3. Angular Example to Render Multiple Rows
  4. Angular @HostListener Decorator With Examples
  5. String Vs StringBuffer Vs StringBuilder in Java
  6. Array in Java
  7. Lazy Initializing Spring Beans
  8. Introduction to Hadoop Framework

No comments:

Post a Comment