Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does bootstrap function do in Angular

Tags:

angular

I'm very new to frontend frameworks and I am learning Angular 2. In the tutorial, it tells you to include the line bootstrap(AppComponent). What does this bootstrap function do? Is it simply what starts the app? I'm guessing it has nothing to do with the twitter-bootstrap UI framework.

like image 644
xeroshogun Avatar asked Feb 26 '16 21:02

xeroshogun


People also ask

What is the role of bootstrap in Angular?

The bootstrap property or key of the NgModule decorator specifies which component should be loaded by Angular when the app-level module loads. Angular reads the bootstrap metadata and loads the app-level component, called AppComponent .

What is bootstrap component in Angular?

A bootstrapped component is an entry component that Angular loads into the DOM during the bootstrap process (application launch). Other entry components are loaded dynamically by other means, such as with the router. Angular loads a root AppComponent dynamically because it's listed by type in @NgModule. bootstrap .

Is bootstrap necessary for Angular?

Angular Material and Flex Layout are absolutely better than bootstrap. But that doesn't mean that you should not use bootstrap, but you may consider utilizing both the libraries.


4 Answers

From the docs:

You instantiate an Angular application by explicitly specifying a component to use as the root component for your application via the bootstrap() method.

So yes, it just starts the application.

like image 118
Quentin Avatar answered Sep 24 '22 06:09

Quentin


bootstrap() initializes an Angular application by executing (beside others)

  • creating the Angular zone,
  • creating the root injector and
  • executing factories provided by APP_INITIALIZER
  • instantiating and adding the root component.
like image 33
Günter Zöchbauer Avatar answered Sep 23 '22 06:09

Günter Zöchbauer


bootstrap is the function which tells Angular2 system to render component over page as main component.

Also defines entity point of your application, by specifying root of your application.

//basically array will have dependencies of shared component which will instantiate only once.
bootstrap(MyComponent, [SharedService, OtherComponent, ROUTING_DIRECTIVES]); 

But yes you should have mention that component selector over index.html page like

<my-component></my-component>

If you compare this with Angular 1, you will find ng-app directive which takes angular.module name as an input like ng-app="myApp" and make available those module component for that application OR angular.bootstrap function to kick off application over the page.

like image 32
Pankaj Parkar Avatar answered Sep 23 '22 06:09

Pankaj Parkar


Basically bootstrap() in angular2 tell us the Entry point for the app very similer to ng-app in angular 1.x, it creates angular zone for the whole app,In Angular 1.x we could use the ng-app Directive, and give it a value such as ng-app="myApp", or use the angular.bootstrap method which allows for asynchronous bootstrapping.

The place we need to fetch the bootstrap method is angular2/platform/browser

  import {bootstrap} from 'angular2/platform/browser';
  ... Some Code stuff

  bootstrap(AppComponent, [Common providers, or Global services, varibale etc]);

also we can inject GlobalServices, variables which we are going to use in the whole app at the time of bootstraping our app, by doing so we dont nees to imports those again and again in our components.

like image 21
Pardeep Jain Avatar answered Sep 25 '22 06:09

Pardeep Jain