Angular Two-Way Binding: [(ngModel)]
![Angular Two-Way Binding: [(ngModel)]](/_next/image?url=https%3A%2F%2Fangular.dev%2Fassets%2Fimages%2Fng-image.jpg&w=1600&q=75)
Think of [(ngModel)] as a walkie-talkie for data, syncing a component property with a template view. It's used in forms where user input immediately updates a variable, and vice-versa. The footgun is overusing it, which creates tangled data flows.
Why it exists
Building interactive forms requires keeping the user interface (the view) and the application's internal state (the model) in perfect sync. Manually writing event listeners for every keystroke and then separately writing code to update the input's value is repetitive and error-prone. Two-way binding was created to automate this synchronization.
The mental model
Imagine a walkie-talkie connecting your component's data property (like username) and an input field in your HTML. When the user types in the input, they're talking into one walkie-talkie, and the username property hears it and updates. When your code changes the username property, it talks into its walkie-talkie, and the input field on the screen updates. They are always in sync.
How it works
The [(ngModel)] syntax is actually a shortcut, or "syntactic sugar," for two separate bindings. The square brackets [ngModel] handle property binding, pushing data from the component to the view. The parentheses (ngModelChange) handle event binding, listening for changes in the view and pushing them back to the component. Together, [()], often called "banana in a box," creates the two-way flow. To use it, you must also import the FormsModule into your Angular module.
When to use it
Use [(ngModel)] for simple form-based scenarios where immediate, direct synchronization between a view element and a component property is desired. It's perfect for things like settings toggles, search bars, or basic data entry fields in a prototype or a small, self-contained component.
When not to use it
Avoid [(ngModel)] in complex components or when managing shared state. Its implicitness can create hard-to-debug situations where you don't know what caused a state change. In larger applications, a more explicit, one-way data flow (using property binding for input and event binding for output separately) is often preferred for maintainability and clearer state management.
One canonical example
To bind a user's name to an input field, you would have this in your component's TypeScript file: export class UserProfileComponent { username: string = 'Alice'; }. In your corresponding HTML template, you would write: <input [(ngModel)]="username">. Now, the input field will initially display "Alice". If the user types "Bob" into the field, the username property in the component will automatically become "Bob".
Interview question
Which Angular approach is best for quickly synchronizing a simple form input with a component property, ensuring immediate updates in both directions?
- a.Using separate property binding ([value]) and event binding ((input)).
- b.Applying two-way data binding with [(ngModel)].Correct
- c.Relying solely on interpolation ({{ property }}) for display.
- d.Implementing custom event emitters and @Input() decorators.
Why? this is the answer
The card states that [(ngModel)] is "perfect for things like settings toggles, search bars, or basic data entry fields in a prototype or a small, self-contained component" because it automates the synchronization. While separate property and event binding (Option A) can achieve two-way flow, [(ngModel)] is the concise, direct solution for simple scenarios.
Just read this? Test yourself on what you have been reading.
Read the original → angular.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.
See open roles