Data Binding is a technique used to automatically synchronize data between the model and view components. Angular supports both one-way and two-way data binding, making the code more loosely coupled and easier to manage.
Data binding can be categorized into 2 types:
- One-way Binding
- Two-way Binding
Difference between One-way & Two-way Binding
One-way Binding | Two-way Binding |
|---|---|
In one-way binding, the flow is one-directional. | In a two-way binding, the flow is two-directional. |
This means that the flow of code is from TypeScript file to the HTML file. | This means that the flow of code is from TypeScript file to HTML file as well as from HTML file to TypeScript file. |
In order to achieve one-way binding, we used the property binding concept in Angular. | In order to achieve a two-way binding, we will use ngModel or banana in a box syntax. |
In property binding, we encapsulate the variable in HTML with square brackets( [ ] ). | Import |
Changes in the view are not reflected back in the component class. | Changes in the view update the component, and component changes update the view. |
Uses property binding only. | Uses |
Use Cases of One-way Binding
Used when data needs to flow only from the component to the view. The general use cases are:
- Displaying values: Used to show data such as usernames, prices, titles, or messages in the user interface.
- Read-only information: Suitable for content that users can view but should not modify, such as reports or status messages.
- Passing data to child components: Helps transfer data from a parent component to a child component using
@Input(). - Dynamic attribute binding: Used to dynamically set properties like image sources, button states, styles, and CSS classes.
import { Component } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent {
title = "Displaying the content with one way binding";
}
<h3>Displaying the content without one way binding</h3>
<hr />
<h3 [textContent]="title"></h3>
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { AppComponent } from "./app.component";
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Output:

Use cases of Two-way binding:
Used when data in the component and view must stay synchronized. The general use cases are:
- Form handling: Commonly used with text fields, checkboxes, radio buttons, and dropdown menus.
- Live search and filtering: Allows search results or filtered content to update instantly as the user types.
- Editable forms and settings: Useful in profile pages, feedback forms, and settings panels where users frequently modify data.
- Real-time synchronization: Ensures that any change in the UI updates the component data and any change in the component updates the UI automatically.
import { Component } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
})
export class AppComponent {
data = "Ram and Syam";
}
<input [(ngModel)]="data" type="text">
<hr>
<h3> Entered data is {{data}}</h3>
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Output:
