How many types of data bindings in AngularJS ?
Last Updated :
09 May, 2022
In this article, we will know about the concept of data binding, along with different types of binding available in AngularJS.
The Data Binding refers to the synchronization of data between the model and view. Synchronizing data is imperative for keeping the data being displayed to the user and the data being stored updated at all times. In AngularJS, Data Binding is an important concept as it acts as a bridge between the view and the logic of the AngularJS app. Data Binding In AngularJS is achieved by using Directives.
There are 2 major components of Data Binding in AngularJS:
- Model: It is responsible for maintaining data in the application.
- View: It is the HTML container where the app is displayed to the user.
AngularJS provides two types of Data Binding:
- One-way data binding
- Two-way data binding
We will discuss both of them in detail with examples.
One Way Data Binding: In one-way data binding, the flow of data is in one direction only i.e. from model to view. A value is taken from the data model, inserted in an HTML element, and displayed to the user. But there is no way to update the model according to the input given by the user which means that the data can't flow from the view to the model.
One-Way Data Binding can be achieved by:
- Interpolation
- Using ng-bind directive
Interpolation: Interpolation is a one-way data-binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view.
Syntax:
{{expression}}
Example: This example describes the One-way data binding using double braces expressions.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Data Binding Example</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<h1>{{title1}}</h1>
<h2>{{title2}}</h2>
<p>{{description}}</p>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.title1 = "GeeksforGeeks";
$scope.title2 = "AngularJs Data Binding";
$scope.description ="Data Binding refers "
+ "to the synchronization of data "
+ "between the model and view";
});
</script>
</body>
</html>
Explanation: In the above example, we have used expressions to bind the data from the model to the view. In the <script> tag we have created a module named "myApp". To this module, we have added a controller "myCtrl". We have added properties to this controller like title1, title2, and description. Then we have specified these properties in the HTML elements where we want to show the data to the users.
Output:
Using ng-bind directive: The ng-bind Directive in AngularJS is used to bind/replace the text content of any particular HTML element with the value that is entered in the given expression. The value of specified HTML content updates whenever the value of the expression changes in ng-bind directive.
Syntax:
<element ng-bind="expression"> Contents... </element>
Example: This example describe the One-way data binding using ng-bind directive.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Data Binding Example</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<p>Firstname:
<span ng-bind="firstname"></span>
</p>
<p>Lastname:
<span ng-bind="lastname"></span>
</p>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstname = "GeeksforGeeks";
$scope.lastname = "AngularJS";
});
</script>
</body>
</html>
Explanation: In the above example, we have used ng-bind to bind data from the model to the view. In the <script> tag we have added properties to the controller like firstname and lastname. We have specified the ng-bind directive in the HTML tags where we want to display the data. In the ng-bind directive, we have specified the property we want to display.
Output:
Data Binding using the ng-bind directiveTwo Way Data Binding: In this type of data binding, the flow of data is bidirectional i.e the data can flow from the model to the view as well as from the view to the model. In simple words, we can say that when the data in the model changes, the changes are reflected in the view and when the data in the view changes the model is also updated. The view and model are updated at all times.
Two-way data binding is achieved by using the ng-model directive. The ng-model directive transfers data from the controller to the view and vice versa.
Example: In this example, we have created a form asking for the user's name, age, and the course they are interested in. After the user gives the input for all the fields and clicks on the submit button the details entered by the user are displayed.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Data Binding Example</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<form>
<label>Enter your name:</label>
<input type="text" ng-model="name">
<br>
<label>Enter your age:</label>
<input type="number" ng-model="age">
<br>
<label>Enter the course you are interested in:</label>
<input type="text" ng-model="course">
<br>
<input type="submit" ng-click="details()">
</form>
<div ng-show="showdetails">
<h1>Details entered by the user:</h1>
<p>Name: {{name}}</p>
<p>Age: {{age}}</p>
<p>Course: {{course}}</p>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.showdetails = false;
$scope.details = function() {
$scope.showdetails = true;
}
});
</script>
</body>
</html>
Explanation: We have specified the ng-model directive in all the input fields so as to create a model property for each of them. Initially in the controller, the value for the property "details" is false. After the submit button is clicked, the function details() is called which changes the value of the property "details" to true. In the <div> tag we have specified the ng-show directive which shows the <div> tag if the value of "details" is true. So as the user enters the details and clicks on the submit button the details are displayed using expressions.
Output:
Two Way Data Binding ExampleExample: In this example, the user is supposed to enter the quantity for each product they want to order, and accordingly the price will be calculated and displayed to the user.
HTML
<!DOCTYPE html>
<html>
<head>
<title>>Data Binding Example</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<style>
table {
border: 1px solid black;
border-collapse: collapse;
}
tr, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
}
</style>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<table>
<tr>
<th>Food Items</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<tr>
<td>Pizza</td>
<td>100</td>
<td>
<input type="number"
ng-model="pizza">
</td>
</tr>
<tr>
<td>Pasta</td>
<td>150</td>
<td>
<input type="number"
ng-model="pasta">
</td>
</tr>
<tr>
<td>Garlic Bread</td>
<td>170</td>
<td>
<input type="number"
ng-model="garlicbread">
</td>
</tr>
<tr>
<td>Nachos</td>
<td>200</td>
<td>
<input type="number"
ng-model="nachos">
</td>
</tr>
<tr>
<td>Ice Cream</td>
<td>250</td>
<td>
<input type="number"
ng-model="icecream">
</td>
</tr>
</table>
<br>
<button ng-click="calculate()">
Place Order
</button>
<div ng-show="amt!=0">
<h1>
Total amount to be paid: {{amt}}
</h1>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.title = "Food ordering";
$scope.amt = 0;
$scope.calculate = function() {
$scope.amt = 100 * $scope.pizza
+ 150 * $scope.pasta
+ 170 * $scope.garlicbread
+ 200 * $scope.nachos
+ 250 * $scope.icecream;
}
});
</script>
</body>
</html>
Explanation: In the above example, we have used data binding in order to take data from the user from the view, pass it to the controller, the controller performs the processing using the model properties and then passes it to the view for the data to be shown to the users.
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read