AngularJS is a very powerful JavaScript Framework. It is used in Single Page Application (SPA) projects. It extends HTML DOM with additional attributes and makes it more responsive to user actions. AngularJS is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache license version 2.0.
Audience
This tutorial is designed for software professionals who want to learn the basics of AngularJS and its programming concepts in simple and easy steps. It describes the components of AngularJS with suitable examples.
Prerequisites
You should have a basic understanding of JavaScript and any text editor. As we are going to develop web-based applications using AngularJS, it will be good if you have an understanding of other web technologies such as HTML, CSS, AJAX, etc.
AngularJS is an open-source web application framework. It was originally developed in 2009 by Misko Hevery and Adam Abrons. It is now maintained by Google. Its latest version is 1.2.21.
Definition of AngularJS as put by its official documentation is as follows −
AngularJS is a structural framework for dynamic web applications. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application components clearly and succinctly. Its data binding and dependency injection eliminate much of the code you currently have to write. And it all happens within the browser, making it an ideal partner with any server technology.
General Features
The general features of AngularJS are as follows −
Overall, AngularJS is a framework to build large scale, high-performance, and easyto-maintain web applications.
Core Features
The core features of AngularJS are as follows −
Concepts
The following diagram depicts some important parts of AngularJS which we will discuss in detail in the subsequent chapters.
Advantages of AngularJS
The advantages of AngularJS are −
On the top of everything, AngularJS applications can run on all major browsers and smart phones, including Android and iOS based phones/tablets.
Disadvantages of AngularJS
Though AngularJS comes with a lot of merits, here are some points of concern −
AngularJS Directives
The AngularJS framework can be divided into three major parts −
This chapter describes how to set up AngularJS library to be used in web application development. It also briefly describes the directory structure and its contents.
When you open the link https://angularjs.org/, you will see there are two options to download AngularJS library −
This screen gives various options of using Angular JS as follows −
We are using the CDN versions of the library throughout this tutorial.
Example
Now let us write a simple example using AngularJS library. Let us create an HTML file myfirstexample.html shown as below −
<!doctype html> <html> <head> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js"></script> </head><body ng-app = “myapp”>
<div ng-controller = “HelloController” >
<h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2>
</div><script> angular.module("myapp", []) .controller("HelloController", function($scope) { $scope.helloTo = {}; $scope.helloTo.title = "AngularJS"; }); </script>
</body>
</html>
Let us go through the above code in detail −
We include the AngularJS JavaScript file in the HTML page so that we can use it −
<head>
<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js”>
</script>
</head>
You can check the latest version of AngularJS on its official website.
Next, it is required to tell which part of HTML contains the AngularJS app. You can do this by adding the ng-app attribute to the root HTML element of the AngularJS app. You can either add it to the html element or the body element as shown below −
<body ng-app = “myapp”>
</body>
The view is this part −
<div ng-controller = “HelloController” >
<h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2>
</div>
ng-controller tells AngularJS which controller to use with this view. helloTo.title tells AngularJS to write the model value named helloTo.title in HTML at this location.
The controller part is −
<script>
angular.module(“myapp”, []).controller(“HelloController”, function($scope) {
$scope.helloTo = {};
$scope.helloTo.title = “AngularJS”;
});
</script>
This code registers a controller function named HelloController in the angular module named myapp. We will study more about modules and controllers in their respective chapters. The controller function is registered in angular via the angular.module(…).controller(…) function call.
The $scope parameter model is passed to the controller function. The controller function adds a helloTo JavaScript object, and in that object it adds a title field.
Save the above code as myfirstexample.html and open it in any browser. You get to see the following output −
Welcome AngularJS to the world of Tutorialspoint!
What happens when the page is loaded in the browser ? Let us see −
Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts −
MVC is popular because it isolates the application logic from the user interface layer and supports separation of concerns. The controller receives all requests for the application and then works with the model to prepare any data needed by the view. The view then uses the data prepared by the controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows.
The model is responsible for managing application data. It responds to the request from view and to the instructions from controller to update itself.
The View
A presentation of data in a particular format, triggered by the controller’s decision to present the data. They are script-based template systems such as JSP, ASP, PHP and very easy to integrate with AJAX technology.
The Controller
The controller responds to user input and performs interactions on the data model objects. The controller receives input, validates it, and then performs business operations that modify the state of the data model.
AngularJS is a MVC based framework. In the coming chapters, we will see how AngularJS uses MVC methodology.
Before creating actual Hello World ! application using AngularJS, let us see the parts of a AngularJS application. An AngularJS application consists of following three important parts −
Creating AngularJS Application
Being a pure JavaScript framework, it can be added using <Script> tag.
<script
src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”>
</script>
<div ng-app = “”>
…
</div>
<p>Enter your Name: <input type = “text” ng-model = “name”></p>
<p>Hello <span ng-bind = “name”></span>!</p>
Executing AngularJS Application
Use the above-mentioned three steps in an HTML page.
<html>
<head>
<title>AngularJS First Application</title>
</head><body>
<h1>Sample Application</h1><div ng-app = ""> <p>Enter your Name: <input type = "text" ng-model = "name"></p> <p>Hello <span ng-bind = "name"></span>!</p> </div> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser. Enter your name and see the result.
How AngularJS Integrates with HTML
AngularJS directives are used to extend HTML. They are special attributes starting with ng-prefix. Let us discuss the following directives −
ng-app directive
The ng-app directive starts an AngularJS Application. It defines the root element. It automatically initializes or bootstraps the application when the web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application. In the following example, we define a default AngularJS application using ng-app attribute of a <div> element.
<div ng-app = “”>
…
</div>
ng-init directive
The ng-init directive initializes an AngularJS Application data. It is used to assign values to the variables. In the following example, we initialize an array of countries. We use JSON syntax to define the array of countries.
<div ng-app = “” ng-init = “countries = [{locale:‘en-US’,name:‘United States’},
{locale:‘en-GB’,name:‘United Kingdom’}, {locale:‘en-FR’,name:‘France’}]”>
…
</div>
ng-model directive
The ng-model directive defines the model/variable to be used in AngularJS Application. In the following example, we define a model named name.
<div ng-app = “”>
…
<p>Enter your Name: <input type = “text” ng-model = “name”></p>
</div>
ng-repeat directive
The ng-repeat directive repeats HTML elements for each item in a collection. In the following example, we iterate over the array of countries.
<div ng-app = “”>
…
<p>List of Countries with locale:</p><ol>
<li ng-repeat = “country in countries”>
{{ 'Country: ’ + country.name + ', Locale: ’ + country.locale }}
</li>
</ol>
</div>
Example
The following example shows the use of all the above-mentioned directives.
<html>
<head>
<title>AngularJS Directives</title>
</head><body>
<h1>Sample Application</h1><div ng-app = "" ng-init = "countries = [{locale:'en-US',name:'United States'}, {locale:'en-GB',name:'United Kingdom'}, {locale:'en-FR',name:'France'}]"> <p>Enter your Name: <input type = "text" ng-model = "name"></p> <p>Hello <span ng-bind = "name"></span>!</p> <p>List of Countries with locale:</p> <ol> <li ng-repeat = "country in countries"> {{ 'Country: ' + country.name + ', Locale: ' + country.locale }} </li> </ol> </div> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</body>
</html>
Open the file testAngularJS.htm in a web browser. Enter your name and see the result.
Expressions are used to bind application data to HTML. Expressions are written inside double curly braces such as in {{ expression}}. Expressions behave similar to ngbind directives. AngularJS expressions are pure JavaScript expressions and output the data where they are used.
Using numbers
<p>Expense on Books : {{cost * quantity}} Rs</p>
Using Strings
<p>Hello {{student.firstname + " " + student.lastname}}!</p>
Using Object
<p>Roll No: {{student.rollno}}</p>
Using Array
<p>Marks(Math): {{marks[3]}}</p>
The following example shows the use of all the above-mentioned expressions −
<html>
<head>
<title>AngularJS Expressions</title>
</head><body>
<h1>Sample Application</h1><div ng-app = "" ng-init = "quantity = 1;cost = 30; student = {firstname:'Mahesh',lastname:'Parashar',rollno:101}; marks = [80,90,75,73,60]"> <p>Hello {{student.firstname + " " + student.lastname}}!</p> <p>Expense on Books : {{cost * quantity}} Rs</p> <p>Roll No: {{student.rollno}}</p> <p>Marks(Math): {{marks[3]}}</p> </div> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser and see the result.
AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is defined using ng-controller directive. A controller is a JavaScript object that contains attributes/properties, and functions. Each controller accepts $scope as a parameter, which refers to the application/module that the controller needs to handle.
<div ng-app = “” ng-controller = “studentController”>
…
</div>
Here, we declare a controller named studentController, using the ng-controller directive. We define it as follows −
<script>
function studentController($scope) {
$scope.student = {
firstName: “Mahesh”,
lastName: “Parashar”,fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } };
}
</script>
Now we can use studentController’s student property using ng-model or using expressions as follows −
Enter first name: <input type = “text” ng-model = “student.firstName”><br>
Enter last name: <input type = “text” ng-model = “student.lastName”><br>
<br>
You are entering: {{student.fullName()}}
Example
The following example shows the use of controller −
<html>
<head>
<title>Angular JS Controller</title>
<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”>
</script>
</head><body>
<h2>AngularJS Sample Application</h2><div ng-app = "mainApp" ng-controller = "studentController"> Enter first name: <input type = "text" ng-model = "student.firstName"><br> <br> Enter last name: <input type = "text" ng-model = "student.lastName"><br> <br> You are entering: {{student.fullName()}} </div> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('studentController', function($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; }); </script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser and see the result.
Filters are used to modify the data. They can be clubbed in expression or directives using pipe (|) character. The following list shows the commonly used filters.
Uppercase Filter
Add uppercase filter to an expression using pipe character. Here we’ve added uppercase filter to print student name in all capital letters.
Enter first name:<input type = “text” ng-model = “student.firstName”>
Enter last name: <input type = “text” ng-model = “student.lastName”>
Name in Upper Case: {{student.fullName() | uppercase}}
Lowercase Filter
Add lowercase filter to an expression using pipe character. Here we’ve added lowercase filter to print student name in all lowercase letters.
Enter first name:<input type = “text” ng-model = “student.firstName”>
Enter last name: <input type = “text” ng-model = “student.lastName”>
Name in Lower Case: {{student.fullName() | lowercase}}
Currency Filter
Add currency filter to an expression returning number using pipe character. Here we’ve added currency filter to print fees using currency format.
Enter fees: <input type = “text” ng-model = “student.fees”>
fees: {{student.fees | currency}}
Filter
To display only required subjects, we use subjectName as filter.
Enter subject: <input type = “text” ng-model = “subjectName”>
Subject:
<ul>
<li ng-repeat = “subject in student.subjects | filter: subjectName”>
{{ subject.name + ‘, marks:’ + subject.marks }}
</li>
</ul>
OrderBy Filter
To order subjects by marks, we use orderBy marks.
Subject:
<ul>
<li ng-repeat = “subject in student.subjects | orderBy:‘marks’”>
{{ subject.name + ‘, marks:’ + subject.marks }}
</li>
</ul>
Example
The following example shows use of all the above mentioned filters.
<html>
<head>
<title>Angular JS Filters</title>
<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”>
</script>
</head><body>
<h2>AngularJS Sample Application</h2><div ng-app = "mainApp" ng-controller = "studentController"> <table border = "0"> <tr> <td>Enter first name:</td> <td><input type = "text" ng-model = "student.firstName"></td> </tr> <tr> <td>Enter last name: </td> <td><input type = "text" ng-model = "student.lastName"></td> </tr> <tr> <td>Enter fees: </td> <td><input type = "text" ng-model = "student.fees"></td> </tr> <tr> <td>Enter subject: </td> <td><input type = "text" ng-model = "subjectName"></td> </tr> </table> <br/> <table border = "0"> <tr> <td>Name in Upper Case: </td><td>{{student.fullName() | uppercase}}</td> </tr> <tr> <td>Name in Lower Case: </td><td>{{student.fullName() | lowercase}}</td> </tr> <tr> <td>fees: </td><td>{{student.fees | currency}} </td> </tr> <tr> <td>Subject:</td> <td> <ul> <li ng-repeat = "subject in student.subjects | filter: subjectName |orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul> </td> </tr> </table> </div> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('studentController', function($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fees:500, subjects:[ {name:'Physics',marks:70}, {name:'Chemistry',marks:80}, {name:'Math',marks:65} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; }); </script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser. See the result.
Table data is generally repeatable. The ng-repeat directive can be used to draw table easily. The following example shows the use of ng-repeat directive to draw a table −
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr><tr ng-repeat = “subject in student.subjects”>
<td>{{ subject.name }}</td>
<td>{{ subject.marks }}</td>
</tr>
</table>
Table can be styled using CSS Styling.
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f2f2f2;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
Example
The following example shows the use of all the above-mentioned directives.
<html>
<head>
<title>Angular JS Table</title>
<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”></script><style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f2f2f2; } table tr:nth-child(even) { background-color: #ffffff; } </style>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app = “mainApp” ng-controller = “studentController”><table border = "0"> <tr> <td>Enter first name:</td> <td><input type = "text" ng-model = "student.firstName"></td> </tr> <tr> <td>Enter last name: </td> <td> <input type = "text" ng-model = "student.lastName"> </td> </tr> <tr> <td>Name: </td> <td>{{student.fullName()}}</td> </tr> <tr> <td>Subject:</td> <td> <table> <tr> <th>Name</th>. <th>Marks</th> </tr> <tr ng-repeat = "subject in student.subjects"> <td>{{ subject.name }}</td> <td>{{ subject.marks }}</td> </tr> </table> </td> </tr> </table> </div> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('studentController', function($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fees:500, subjects:[ {name:'Physics',marks:70}, {name:'Chemistry',marks:80}, {name:'Math',marks:65}, {name:'English',marks:75}, {name:'Hindi',marks:67} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; }); </script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser and see the result.
Thanks For Visiting, Keep Visiting.
continue…
#angular-js #angular #web-development #devops