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

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 forngModel
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
Concept | Description |
[(ngModel)] | Two-way binding between UI and class |
FormsModule | Required to use ngModel in Angular |
name attribute | Required 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! ๐
Subscribe to my newsletter
Read articles from Tushar Jadhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
