What is Do Bootstrap (ng Do Bootstrap) In Angular 7?

The ng Do Bootstrap is a new life-cycle hook added in Angular 7 and Do Bootstrap is an interface.

In Angular 7, ngDoBootstrap is a lifecycle hook method defined in the ApplicationRef interface. This method is used to manually bootstrap Angular applications when they are not bootstrapped automatically.

Here’s a breakdown of its functionality:

  1. Manual Bootstrap: Angular applications are typically bootstrapped automatically using the platformBrowserDynamic().bootstrapModule() function in the main.ts file. However, in certain scenarios, you may want to bootstrap the application manually.
  2. Custom Initialization Logic: When you implement the ngDoBootstrap method in your root module, Angular calls this method during the application bootstrap process. This gives you an opportunity to perform custom initialization logic before the application starts.
  3. Multiple Bootstrapping: Angular allows multiple applications to coexist within a single page. When you have multiple root modules in your Angular application, you can specify which modules to bootstrap manually using ngDoBootstrap.

Here’s a simple example of how ngDoBootstrap can be implemented in an Angular 7 application:

typescript
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [], // No automatic bootstrap
})
export class AppModule {
constructor(private appRef: ApplicationRef) {}

ngDoBootstrap() {
// Manually bootstrap the AppComponent
this.appRef.bootstrap(AppComponent);
}
}

In this example, the AppModule does not specify any components for automatic bootstrap (bootstrap: []). Instead, it implements the ngDoBootstrap method to manually bootstrap the AppComponent when the application starts.

So, to summarize, ngDoBootstrap in Angular 7 provides a mechanism for manual bootstrapping of Angular applications and allows for custom initialization logic to be executed before the application starts.