Introduction
I was searching for new articles in C# Corner and got a chance to read an article displaying alerts in AngularJS. The article was nice and I read a comment for that article stating that "I never tried AngularJS without 'ng-app', so will it work, if yes then why? pls answer" by Sumit Jolly.
I replied there "AngularJS will work perfectly without using ng-app". So I will now show you guys how an Angular app will work without a ng-app directive, in other words, how to initialize an Angular app.
There are two ways in which we can initialize the Angular app. The most simple and static way is to use the ng-app directive in the root element. And the second method is to bootstrap the Angular app using Angular's bootstrap method. Let us explore these two with examples.
Automatic Initialization
In automatic initialization, when the DOM is loaded, in other words document.readyState == "complete" or when the DOMContentLoaded event is fired, the Angular looks for the ng-app directive.
If it finds the ng-app directive then it loads the modules associated with it and it does some injection magic for you and Angular sets the app ready.
Example:
In the code above, the module name is options since we don't have anything to do with the module.
Manual Initialization
You can manually initialize the Angular app using Angular's bootstrap function. In manual initialization you have more control over the init process.
Example:
In the code above, we have not used the ng-app directive in any of the elements. But we will be initializing the app from JavaScript.
In the app.js file:
As you can see, we can initialize the Angular app using the bootstrap function. The bootstrap function accepts two arguments, the first is the element to be initialized as the root and the second argument accepts an array of modules to be bound with.
Note:
You can bootstrap more that one element as a root but you can't bootstrap an element that is a child of a bootstrapped element. And you are not allowed to do automatic initialization as well as manual.
Example 1
Example 2
In the preceding examples, you cannot bootstrap the element "child" if you have bootstrapped the element "parent" already.
I replied there "AngularJS will work perfectly without using ng-app". So I will now show you guys how an Angular app will work without a ng-app directive, in other words, how to initialize an Angular app.
There are two ways in which we can initialize the Angular app. The most simple and static way is to use the ng-app directive in the root element. And the second method is to bootstrap the Angular app using Angular's bootstrap method. Let us explore these two with examples.
Automatic Initialization
In automatic initialization, when the DOM is loaded, in other words document.readyState == "complete" or when the DOMContentLoaded event is fired, the Angular looks for the ng-app directive.
If it finds the ng-app directive then it loads the modules associated with it and it does some injection magic for you and Angular sets the app ready.
Example:
- <!doctype html>
- <html ng-app="module name is option here">
- <head lang="en">
- <meta charset="utf-8">
- <title>Automatic Initializing</title>
- </head>
- <body>
- 10 * 20 is {{ 10 * 20 }}.
- <script src="angular.js"></script>
- </body>
- </html>
Manual Initialization
You can manually initialize the Angular app using Angular's bootstrap function. In manual initialization you have more control over the init process.
Example:
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="utf-8">
- <title>Manual Initializing</title>
- </head>
- <body>
- <div id="container">
- 10 * 20 is {{ 10 * 20 }}.
- </div>
- <script type="text/javascript" src="angular.min.js"></script>
- <script type="text/javascript" src="app.js"></script>
- </body>
- </html>
In the app.js file:
- var app = angular.module("app", []);
- angular.bootstrap(document.getElementById("container"), ["app"])
Note:
You can bootstrap more that one element as a root but you can't bootstrap an element that is a child of a bootstrapped element. And you are not allowed to do automatic initialization as well as manual.
Example 1
- <div class="parent" >
- <div class="child"> <!-- bootstrapping child is not possible -->
- </div>
- </div>
- <div class="parent" >
- </div>
- <div class="child"> <!-- bootstrapping child is now possible -->
- </div>
Conclusion
In this article, we have discussed the Angular application initialization. Thanks for reading.
Your comments are welcome.
Happy Coding :)

Mohit SharmaPosted May 26, 2016, 6:25 AM
Nice Article
Gaurav Kumar AroraPosted Dec 15, 2014, 3:43 PM
Jaganathan Bantheswaran _ Nice, I follow you. Good to get these contents.
Jaganathan BantheswaranPosted Dec 15, 2014, 2:03 AM
And a final word, there is nothing innovation in this article. Because everything is written in detail already @ angularjs.org. So we have to just explore it.
Jaganathan BantheswaranPosted Dec 13, 2014, 12:13 AM
I agree with you Gaurav. Even I explained the both the ways to bootstrap the angular app. And the angular compiler is just to compile the directives used in the HTML code based on the directive's priority so nothing explicit with ng-app. One more thing, ng-app is the highest priority directive. As I told, you can't use both ng-app as well as bootstrap option.
Gaurav Kumar AroraPosted Dec 12, 2014, 2:54 PM
Jaganathan Bantheswaran - even I did not try this but what if I removed the ng-app from here: <html ng-app ="module name is option here" > so, is it work if <html> is the only thing? I still have few things in my mind. We still need to initialize our angularjs in eitherway by using ng-app directive or using bootrap which accepting two parameters , referring-https://docs.angularjs.org/guide/bootstrap. One more thing I came to know angular bootstraper ultimate uses the same ng-app angular directive to compile html here it is: https://docs.angularjs.org/guide/compiler.
Gaurav Kumar AroraPosted Dec 12, 2014, 2:51 PM
Jaganathan Bantheswaran - as a reply to this article: http://www.c-sharpcorner.com/Blogs/46796/how-do-i-show-alert-in-angularjs.aspx its a great and really said innovative thing. Great to see this
K P Singh ChundawatPosted Nov 22, 2014, 4:37 PM
Jaganathan Bantheswaran Really very good Article ....learn new thing...thanks for sharing