The Wayback Machine - https://web.archive.org/web/20240602030817/https://www.geeksforgeeks.org/difference-between-one-way-binding-and-two-way-binding/
Open In App

Difference between One-way Binding and Two-way Binding

Last Updated : 22 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn the concept of data binding in Angular. We will also explore its types & examine the differences between one-way binding and two-way binding in angular. 

Data binding is a way to synchronise the data between the model and view components automatically. AngularJS implements data-binding that treats the model as the single-source-of-truth in your application & for all the time, the view is a projection of the model. Unlike React, angular supports two-way binding. In this way, we can make the code more loosely coupled. Data binding can be categorized into 2 types, ie., One-way Binding & Two-way Binding.

One way binding:

  • In one-way binding, the data flow is one-directional.
  • This means that the flow of code is from typescript file to Html file.
  • In order to achieve a one-way binding, we used the property binding concept in Angular.
  • In property binding, we encapsulate the variable in Html with square brackets( [ ] ).
  • We will understand this concept through an example in order to make it more comprehensible.

app.component.ts




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";
}


 

app.component.html




<h3>Displaying the content without one way binding</h3>
  
<hr />
  
<h3 [textContent]="title"></h3>


app.module.ts




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:

Two-way binding:

  • In a two-way binding, the data flow is bi-directional.
  • This means that the flow of code is from ts file to Html file as well as from Html file to ts file.
  • In order to achieve a two-way binding, we will use ngModel or banana in a box syntax.
  • To make sure the app doesn’t break, we need to import ‘FormsModule’ from ‘@angular/forms.
  • Any changes to the view are propagated to the component class. Also, any changes to the properties in the component class are reflected in the view.
  • To bind two properties in order to two-way binding works, declare the ngModel directive and set it equal to the name of the property.
  • We will understand the concept through an example in order to make it more comprehensible.

app.component.ts




import { Component } from "@angular/core";
  
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  data = "Ram and Syam";
}


app.component.html




<input [(ngModel)]="data"  type="text">
  
<hr>
  
<h3> Entered data is  {{data}}</h3>


 

app.module.ts




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:

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 ts file to  Html file.

This means that the flow of code is from ts file to Html file as well as from Html file to ts 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( [ ] ).

To make sure the app doesn’t break, we need to import ‘FormsModule’ from ‘@angular/forms’. Using ngModel, we will bind a variable from Html to ts file and from ts file to Html file.



Similar Reads

What is the difference between one-way data flow and two-way data binding in vue.js?
Vue.js is an open-source front-end JavaScript framework for building user interfaces and single-page applications. It follows Model-View-ViewModel (MVVM) architecture pattern. Vue.js has many in-built directives for DOM manipulation such as v-bind, v-on, v-model, etc. In this article, we will learn about the one-way data binding &amp; two-way data
7 min read
Vue.js Two Way Binding Model
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. The v-model directive makes two-wa
2 min read
Two-way Data Binding in AngularJS
In this article, we will see the Data Binding, along with understanding how the flow of code is from a Typescript file to an HTML file &amp; vice-versa through their implementation. In AngularJS, Data Binding refers to the synchronization between the model and view. In Two-way data binding, the flow of data is bidirectional i.e. when the data in th
3 min read
How to achieve Two-Way Data Binding in Angular with ngModel ?
Two-way Data Binding in Angular allows you to automatically synchronize data between a component class property and an input element (e.g., an input field) in your template. To achieve this binding, it's typically used with Angular [(ngModel)] directive. This is basically the combination of the Property Binding &amp; the Event Binding, which helps
3 min read
Two Way Data Binding in javascript
In web development, keeping the user interface (UI) and the underlying data model in sync is crucial for a seamless user experience. Two-way data binding is a technique that automates this synchronization, reducing boilerplate code and simplifying development. What is Two-Way Data Binding?Imagine you have a text input field where users can enter th
4 min read
How to Add Border on Only One Side in Shorter Way in CSS ?
The border is one of the most important styling properties in CSS and it is very crucial to add in most of the layout to feature stark distinctions. But not always required we need to add borders to all sides and maybe to only one side. In this article, we are going to see the shortest way to do that in traditional CSS, Bootstrap 5, and Tailwind CS
3 min read
Vue.js Prop One-Way Data Flow
In VueJS, all the props are bonded between the child and properties is one-way-down. When we update the prop in the parent component, it is updated to all the child components, but the reverse does not happen and VueJS warns the use. This is used as protection from altering the state of a parent-by-child component. Different Cases where the Child C
4 min read
Angular PrimeNG Form RadioButton Dynamic Values, Preselection, Value Binding and Disabled Option
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the Form RadioButton Dynamic Values, Preselection, Value Binding, and Disabled Option in Angular PrimeNG. The
3 min read
Angular PrimeNG Form Checkbox Dynamic Values, Preselection, Value Binding and Disabled Option
Angular PrimeNG is a UI component library for Angular Applications. It offers many pre-built themes and UI components for a variety of tasks like inputs, menus, charts, Buttons, etc. In this article, we will be seeing Angular PrimeNG Form Checkbox Dynamic Values, Preselection, Value Binding, and Disabled Option Component. The Checkbox Component pro
3 min read
AngularJS Data Binding
In this article, we will see the Data Binding in AngularJS, along with understanding the various types of Data Binding available with their implementations. Angular provides a function Data Binding which helps us to have an almost real-time reflection of the input given by the user i.e. it creates a connection between Model and View. Data Binding i
5 min read