Categories: JavascriptWeb

AngularJS – Two Ways to Initialize an Angular App

This article represents code samples along with related concepts for two different ways in which Angular app can be defined. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • Automatic Initialization
  • Manual Initialization

 

Automatic Initialization

All that is needed for automatic initialization (as of current AngularJS version) is to define “ng-app” on an element and you should be all set. Take a look at following code sample. Pay attention to some of the following:

  • ng-app=”HelloApp” defined on div element
  • ng-controller=”HelloCtrl” defined on the same element. It could as well be defined within the inner elements.
<!DOCTYPE html>
<html>
<head>
 <title>Hello Angular</title>
 <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
</head>
<body>
  <div ng-app="HelloApp" ng-controller="HelloCtrl">
    <h1>Hello, {{name}}</h1>
   <form>
    <input type="text" ng-model="name" name="name">
   </form>  
  </div>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
 <script type="text/javascript">
 angular.module('HelloApp', [])
 .controller('HelloCtrl', ['$scope', function($scope){
  $scope.name = "Calvin";
 }])
 </script>
</body>
</html>

As per the description on this page, Angular initializes automatically upon DOMContentLoaded event or when the angular.js script is evaluated if at that time document.readyState is set to ‘complete’. At this point, Angular looks for the ng-app directive which designates your application root. Read further on the given page.

 

Manual Initialization

One could also initialize the angular app manually. This seems to be suited more for existing/legacy web pages where you would want to initialize angular app for a particular set of “views” or, initialize the app conditionally. Following is the code sample. Pay attention to the following code snippet which is used to instantiate the angular app manually. angular.bootstrap(document, [‘HelloApp’]);. Read further on this page.

<!DOCTYPE html>
<html>
<head>
 <title>Hello Angular</title>
 <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
</head>
<body>
  <div ng-controller="HelloCtrl">
    <h1>Hello, {{name}}</h1>
   <form>
    <input type="text" ng-model="name" name="name">
   </form>  
  </div>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
 <script type="text/javascript">
 angular.module('HelloApp', [])
 .controller('HelloCtrl', ['$scope', function($scope){
  $scope.name = "Calvin";
 }])
  angular.bootstrap(document, ['HelloApp']);
 </script>
</body>
</html>

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar
Tags: angularjs

Recent Posts

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

13 hours ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

13 hours ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

13 hours ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

5 days ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

7 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

1 week ago