Bootstraping an angular application is as simple as making coffee for yourself.
There are two ways to bootStrap Angular Application.
- Automatic BootStrap (Coffee by Machine)
- Manual BootStrap (Handmade coffee, you can face some trouble)
Automatic BootStrap:
When DOM content is loaded , Angular looks for the ngApp directive which designates application root.
If it finds ngApp directive
a. It loads the module associated with this directive.
e.g.
- <html ng-app='myApp'>
-
- <head>
- <script src='angular.js'>
- </script>
- <script>
- var app = angular.module('myApp', []);
- app.controller('myController', function($scope) {
- $scope.message = 'Dear';
- });
- </script>
- </head>
-
- <body>
- <div ng-controller="myController">
- <p> Hi {{ message}} </p>
- </div>
From the above script, It will load "myApp".
b. Then, it creates the application injector.
c. Lastly, It compiles the DOM where ngApp directive as the root of the compilation.
This whole process says that Only a portion of the DOM is being treated as an Angular application.
As both Automatic and Manual process of bootStraping of an Angular application are different one. Remember to not mix these two concepts together.
These are two ways to do the same processing.
Now let's move to the Manual process of bootstraping an Angular application.
Manual Bootstrap:
There is a big difference in Automatic and manual Bootstrap.
- You do not need to attach ng-app directive with the html element.
- You call function angular.bootstrap(document,['myApp']).
- <!doctype html>
- <html>
-
- <body>
- <div ng-controller="myController"> Best movie: {{MovieName}}! </div>
- <script src='angular.js'>
- </script>
- <script>
- angular.module('myApp', []).controller('MyController', ['$scope', function($scope)
- {
- $scope.MovieName = 'The IRON MAN';
- }]);
- angular.element(document).ready(function()
- {
- angular.bootstrap(document, ['myApp']);
- });
- </script>
- </body>
-
- </html>
Angular.bootstrap can not create module for you untill you do not make a custom module to give it as a parameter inside.
Before bootstraping the process you need to add controllers, directives and services etc.
Manual bootstrap comes into picture when you need more control over the initialization process. like if you want to perform an operation before
Angular compiles a page.
NOTE: You should not use ng-app directive in case of manual bootstrap of an angular application.