Sitemap

Two-way binding in Angular

3 min readJul 8, 2024

--

In my previous articles, I thoroughly explored data binding and events with detailed examples. One-way data binding utilizes interpolation and property binding to maintain a unidirectional flow of data, moving from the component to the view or vice versa. Events in Angular enable applications to react to user interactions like clicks and inputs, which are essential for creating dynamic and responsive user experiences. But can these features be combined?

The answer is yes!

In this article, let’s delve into a feature that combines the strengths of one-way data binding and events, enabling a bidirectional flow of data between the model and the DOM.

We are talking about two-way data binding.

If you’re seeking a video explanation of this concept, click the link below to watch my video on two-way data binding in Angular with examples.

Get InterviewPro’s stories in your inbox

Join Medium for free to get updates from this writer.

https://youtu.be/EpIL1WxXrAc?si=8IQTbZSxdNyHFP4I

Press enter or click to view image in full size

What is Two-Way binding in Angular?

Two-way data binding in Angular makes sure that when you change something in the user interface, it updates the data model right away. Similarly, if you change the data model, it instantly updates what you see on the screen. It merges one-way data binding (where data flows in one direction) and event handling into one method, making it easier to create dynamic and interactive applications.

How does Two-Way data binding work?

In Angular, two-way data binding is commonly done with the ngModel directive found in FormsModule. This directive connects an input field’s value to a property in the component, ensuring that changes in either the view or the model are automatically synchronized.

Syntax:

The syntax for two-way data binding uses a combination of square brackets [] for property binding and parentheses () for event binding, forming [(ngModel)]. This is often referred to as the "banana in a box" syntax because of its appearance.

Example:

<input [(ngModel)]="name" placeholder="Enter your name">
<p>Hello, {{ name }}! Welcome to IntreviewPro.</p>

In this example:

  • The [(ngModel)] directive binds the name property in the component to the input field.
  • When the user types into the input field, the name property in the component is updated in real time.
  • The paragraph element displays the updated name immediately, reflecting changes in the DOM.

How to setup Two-Way binding:

  • Import FormsModule in app.module.ts. This will enable ngModel directive.
app.module.ts
  • Define the property in your component.
app.component.ts
  • Apply [(ngModel)] in the HTML template to bind a form control to a component property.
app.componnet.html
  • Run ng serve to test your changes.
  • Type a name in the input and observe the typed value displayed in the text below.

It’s that simple!

Enjoy coding! Don’t forget to clap and share your thoughts in the comments section.

Follow InterviewPro for more updates. Show your support on my YouTube channel too 😁.

--

--