Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by Bootstrapping in angular JS?

I am a beginner in Angular JS. I was going through the below link. http://docs.angularjs.org/tutorial/step_00

What are the bootstrap files? Where are they located?

What is automatic booting and manual initialization of bootstrapping? I read the disadvantage of manual initialization as below.. from the link http://docs.angularjs.org/guide/bootstrap

Can anyone explain exactly what is the disadvantage here?

like image 677
sabari Avatar asked Jan 11 '14 03:01

sabari


People also ask

What is bootstrapping called Angular?

The process of loading the index. html page, app-level module, and app-level component is called bootstrapping, or loading the app. In this guide, you will learn about the internals of the bootstrapping process. Angular takes the following steps to bootstrap the application: Load index.

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 .

Do we need bootstrap in Angular?

Front-end application development tools like Angular Material and Bootstrap are necessary to create a high-functioning and responsive website. Angular Material is a User Interface module that offers pre-built component collections for frameworks like Vue.

What is bootstrapping of an application?

In computing, the term bootstrap means to boot or to load a program into a computer using a much smaller initial program to load in the desired program, which is usually an OS.


2 Answers

Bootstrapping is the equivalent of initializing, or starting, your Angular app. There are 2 main ways to do so.

The first is automatically bootstrapping by adding ng-app to the an element in your HTML, like so:

<html ng-app="myApp"> ... </html> 

The second would be to bootstrap from the JavaScript, like so, after having creating your app through angular.module("myApp", []);

angular.bootstrap(document, ['myApp']); 
like image 98
knrz Avatar answered Sep 20 '22 12:09

knrz


Though Everyone above has answered perfectly and I found what I was looking for but still a working example seem missing.

While understanding about Auto / Manual bootstrapping in AngularJS below examples can help a lot :

AngularJS : Auto Bootstrapping :

Angular initializes / bootstraps automatically upon DOMContentLoaded event or when the angular.js script is downloaded to the browser and the document.readyState is set to complete. At this point AngularJS looks for the ng-app directive. When the ng-app directive is found then Angular will:

  1. Load the module associated with the directive.

  2. Create the application injector.

  3. Compile the DOM starting from the ng-app root element.

This process is called auto-bootstrapping.

<html>      <body ng-app="myApp">         <div ng-controller="Ctrl">Hello {{msg}}!</div>         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>         <script>             var app = angular.module('myApp', []);             app.controller('Ctrl', function($scope) {                 $scope.msg = 'Nik';             });         </script>     </body>  </html> 

JSFiddle : http://jsfiddle.net/nikdtu/ohrjjqws/

AngularJS - Manual Bootstrapping :

You can manually initialized your angular app by using angular.bootstrap() function. This function takes the modules as parameters and should be called within angular.element(document).ready() function. The angular.element(document).ready() function is fired when the DOM is ready for manipulation.

<html>      <body>         <div ng-controller="Ctrl">Hello {{msg}}!</div>         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>         <script>             var app = angular.module('myApp', []);             app.controller('Ctrl', function($scope) {                 $scope.msg = 'Nik';             });              //manual bootstrap process              angular.element(document).ready(function () { angular.bootstrap(document, ['myApp']); });         </script>     </body>  </html> 

JSFiddle : http://jsfiddle.net/nikdtu/umcq4wq7/

Note :

  1. You should not use the ng-app directive when manually bootstrapping your app.

  2. You should not mix up the automatic and manual way of bootstrapping your app.

  3. Define modules, controller, services etc. before manually bootstrapping your app as defined in above examples.

Reference : http://www.dotnettricks.com/books/angularjs/interview

like image 28
Nikhil Maheshwari Avatar answered Sep 18 '22 12:09

Nikhil Maheshwari