![]()
1. What is Two-Way Data Binding?
Two-way data binding in AngularJS is a powerful feature that automatically synchronizes the Model (JavaScript variables) and the View (HTML).
- When data in the Model changes, the View updates automatically.
- When a user modifies the View (e.g., entering input), the Model updates automatically.
How it Works?
- The Controller sets a variable in the
$scope(Model). - The View (HTML) binds to that variable using
{{ }}orng-model. - When data changes in the Model, the View updates instantly.
- When data is modified in the View, the Model updates instantly.
2. Example of Two-Way Data Binding
Step 1: Create a Basic AngularJS App
app.js (Controller & Model)
var app = angular.module("myApp", []);
app.controller("myController", function($scope) {
// Model
$scope.name = "John Doe";
});
index.html (View)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AngularJS Two-Way Data Binding</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<h2>Two-Way Data Binding Example</h2>
<p>Enter your name:</p>
<input type="text" ng-model="name">
<p>Hello, {{ name }}!</p>
</div>
</body>
</html>
3. Explanation of Code
ng-app="myApp"→ Initializes the AngularJS application.ng-controller="myController"→ Binds the View to the Controller.$scope.name = "John Doe";→ Sets the initial Model value.ng-model="name"→ Binds the input field to$scope.name.{{ name }}→ Displays the value ofnamedynamically.
Result:
- When the user types in the input field, the value updates instantly in the
<p>tag.
4. Benefits of Two-Way Data Binding
- Automatic Synchronization → No need to manually update the View or Model.
- Less Code → Reduces the need for event listeners like
document.getElementById. - Improved User Experience → Real-time updates enhance interactivity.
- Easier Maintenance → The separation of concerns (MVC) makes debugging simple.
5. Traditional JavaScript vs. AngularJS Two-Way Binding
| Feature | Traditional JavaScript (Vanilla JS) | AngularJS Two-Way Binding |
|---|---|---|
| Data Handling | Manual updates using DOM manipulation (document.getElementById) | Automatic synchronization |
| Code Complexity | High (requires event listeners) | Low (handled by AngularJS) |
| Performance | Slower for complex apps | Faster and efficient |
| Ease of Use | Requires extra effort | Simple and automatic |
6. Advanced Example: Two-Way Data Binding with Multiple Fields
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Two-Way Data Binding</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script>
var app = angular.module("myApp", []);
app.controller("myController", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<p>First Name: <input type="text" ng-model="firstName"></p>
<p>Last Name: <input type="text" ng-model="lastName"></p>
<p>Full Name: {{ firstName }} {{ lastName }}</p>
</div>
</body>
</html>
Output:
- As the user types in the First Name and Last Name fields, the Full Name updates instantly.
