๐Ÿงฉ Two-Way Data Binding and Angular Forms in Angular 20

Tushar JadhavTushar Jadhav
3 min read

Sync your UI with your data โ€” instantly!

In this blog, weโ€™ll explore how to bind user input to your component class and keep it updated in real-time using two-way data binding. Weโ€™ll also learn the basics of Angular Forms.


๐Ÿ” What is Two-Way Data Binding?

Two-way binding allows synchronization between the input field in the UI and the component's variable.

This means:

  • When the user updates the input, the component gets updated.

  • When the component value changes, the UI reflects it immediately.

To do this in Angular, we use the [(ngModel)] syntax โ€” also called banana in a box ๐Ÿต๐Ÿ“ฆ.


๐Ÿ› ๏ธ Setting Up ngModel

Before using [(ngModel)], you must import the FormsModule.

Step 1: Import FormsModule

// app.config.ts (Angular 20+)
import { provideForms } from '@angular/forms';

export const appConfig = {
  providers: [provideForms()]
};

โœ… This setup is specific to Angular 20 and standalone components.
You no longer need to import FormsModule from @angular/forms in NgModules.


๐Ÿ“ Simple Two-Way Binding Example

Letโ€™s create a simple input field where users can enter their name and see it update live.

โœ… Component: app.ts

export class App {
  username = '';
}

โœ… Template: app.html

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

๐Ÿ”„ Output:

As you type, the username value updates in real time โ€” both in the input box and in the greeting message.


๐Ÿงช Form Controls with ngModel

You can use [(ngModel)] for multiple fields:

export class App {
  email = '';
  password = '';
}
<form>
  <input type="email" [(ngModel)]="email" name="email" placeholder="Email" />
  <input type="password" [(ngModel)]="password" name="password" placeholder="Password" />
  <button (click)="submitForm()">Submit</button>
</form>

๐Ÿ’ก Don't forget the name attribute โ€” itโ€™s required for ngModel in forms!


โœ… Simple Form Submission

export class App {
  email = '';
  password = '';

  submitForm() {
    console.log('Email:', this.email);
    console.log('Password:', this.password);
  }
}

When you click the button, the current values of the form fields will be logged to the console.


๐Ÿงพ Summary

ConceptDescription
[(ngModel)]Two-way binding between UI and class
FormsModuleRequired to use ngModel in Angular
name attributeRequired in form elements using ngModel

๐Ÿ“Œ Whatโ€™s Next?

In the next blog on The Debug Diaries, weโ€™ll explore:

๐Ÿ”€ Routing and Navigation in Angular 20

Stay tuned and keep debugging with me!


๐Ÿ”— Let's Connect and Explore More!

๐Ÿ“– Read the full series: ngtushar.hashnode.dev/angular-series
๐Ÿ’ป Source Code on GitHub: github.com/Tushar1409-hj
๐Ÿ”— Connect with me on LinkedIn: linkedin.com/in/tushar1409

If you found this post helpful, donโ€™t forget to share it with your fellow developers and follow the series for more Angular insights! ๐Ÿš€

0
Subscribe to my newsletter

Read articles from Tushar Jadhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Jadhav
Tushar Jadhav