Two-Way Data Binding
Learn how Angular keeps component data and user input synchronized using two-way data binding.
What is Two-Way Data Binding?
Two-way data binding allows data to flow in both directions between the component and the template.
When the component value changes, the UI is updated. When the user changes the value in the UI, the component value is also updated.
Two-Way Data Binding Flow
Component
Component contains the application data.
Binding
Angular creates a two-way connection.
Template
Template displays and updates the value.
User
User can change the value through the UI.
Main Two-Way Binding Concepts
ngModel
Commonly used for two-way binding with form controls.
Input
User input can update component data.
UI Update
Component changes are reflected in the UI.
Synchronization
Component and template values stay synchronized.
Two-Way Binding Syntax
Angular commonly uses the banana-in-a-box syntax
[(ngModel)] for two-way binding.
<input [(ngModel)]="property">
The [(ngModel)] syntax combines property binding
and event binding to create two-way data synchronization.
Basic Two-Way Binding Example
export class StudentComponent {
studentName = 'Sam';
}
<input
[(ngModel)]="studentName"
placeholder="Enter student name">
<p>
Student:
{{ studentName }}
</p>
Student: Sam
Updating Component Data from User Input
When the user changes the input value, Angular updates the corresponding component property.
studentName = 'Sam';
<input
[(ngModel)]="studentName">
<p>
Hello {{ studentName }}
</p>
Component
studentName
ngModel
[(ngModel)]
Input
User Value
Two-Way Binding with Multiple Controls
Multiple form controls can be connected to different component properties.
studentName = 'Sam';
course = 'Angular Full Stack';
city = 'Pune';
<input
[(ngModel)]="studentName"
placeholder="Student Name">
<input
[(ngModel)]="course"
placeholder="Course">
<input
[(ngModel)]="city"
placeholder="City">
<p>
Name: {{ studentName }}
</p>
<p>
Course: {{ course }}
</p>
<p>
City: {{ city }}
</p>
Two-Way Binding with Checkbox
Two-way binding can also be used with checkbox controls.
isActive = true;
<input
type="checkbox"
[(ngModel)]="isActive">
<p>
Active:
{{ isActive }}
</p>
Active: true
Two-Way Binding with Select
A select control can also be synchronized with a component property.
selectedCourse = 'Angular';
<select [(ngModel)]="selectedCourse">
<option value="Angular">
Angular
</option>
<option value="Java">
Java
</option>
<option value="DotNet">
.NET
</option>
</select>
Banana-in-a-Box Syntax
The syntax [(ngModel)] is commonly called
"banana-in-a-box" because of the combination of square brackets
and parentheses.
[(ngModel)]="studentName"
Property Binding
[ngModel]
Event Binding
(ngModelChange)
Two-Way Binding
[(ngModel)]
One-Way vs Two-Way Data Binding
| Feature | One-Way Binding | Two-Way Binding |
|---|---|---|
| Direction | One direction | Both directions |
| Common Syntax | {{ value }} |
[(ngModel)] |
| User Input | Does not automatically update property | Updates component property |
| UI Update | Component changes update UI | Component and UI stay synchronized |
| Common Usage | Displaying data | Interactive form controls |
Common Uses of Two-Way Binding
Form Inputs
Synchronize text input values with component properties.
Checkboxes
Keep checkbox state synchronized with component data.
Dropdowns
Synchronize selected values with component properties.
Interactive Forms
Keep user input and application data synchronized.
Complete Two-Way Binding Example
export class StudentComponent {
studentName = 'Sam';
course = 'Angular Full Stack';
}
<h2>
Student Registration
</h2>
<input
[(ngModel)]="studentName"
placeholder="Student Name">
<input
[(ngModel)]="course"
placeholder="Course">
<h4>
Student:
{{ studentName }}
</h4>
<p>
Course:
{{ course }}
</p>
Student Registration
Student: Sam
Course: Angular Full Stack
Best Practices
Use two-way binding when user input needs to update component data immediately.
Two-way binding is especially useful with interactive form controls.
Avoid complex expressions inside two-way bindings.
Remember that two-way binding combines data display and user input updates.
Do not use two-way binding unnecessarily when the data only needs to move in one direction.
Summary
- Two-way binding synchronizes component data and UI input.
[(ngModel)]is commonly used for two-way binding.- Changes in the component are reflected in the UI.
- User input can update the component property.
- Two-way binding is especially useful for forms.
- It combines property binding and event-based updates.