Two-way binding with ngModel lets your app keep the data and the user interface in sync automatically. When the user changes input, the data updates, and when the data changes, the input updates.
Two-way binding with ngModel in Angular
<input [(ngModel)]="propertyName" />The [( )] syntax is called the banana-in-a-box and means two-way binding.
You must import FormsModule in your Angular module to use ngModel.
username variable. Changes in input update username, and changes to username update the input.<input [(ngModel)]="username" />age property inside the user object.<input [(ngModel)]="user.age" type="number" />
selectedColor variable.<select [(ngModel)]="selectedColor"> <option value="red">Red</option> <option value="blue">Blue</option> </select>
This component uses ngModel to bind the input box to the name variable. When you type your name, the greeting updates live below.
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-two-way-binding', standalone: true, imports: [FormsModule], template: ` <label for="nameInput">Enter your name:</label> <input id="nameInput" [(ngModel)]="name" /> <p>Hello, {{ name }}!</p> ` }) export class TwoWayBindingComponent { name = ''; }
Remember to import FormsModule in your Angular module or use standalone components with imports: [FormsModule] to enable ngModel.
Two-way binding is great for simple forms but can be less clear in complex apps where reactive forms or explicit event handling might be better.
Two-way binding with ngModel keeps data and UI in sync automatically.
Use the [(ngModel)] syntax on form controls like inputs and selects.
Make sure to import FormsModule to use ngModel.
Practice
[(ngModel)] directive do in Angular?Solution
Step 1: Understand ngModel purpose
The[(ngModel)]directive binds data both ways: from component to template and back.Step 2: Compare options
Only It creates two-way data binding between the component and the template input. describes two-way binding, while others describe one-way or unrelated behavior.Final Answer:
It creates two-way data binding between the component and the template input. -> Option DQuick Check:
Two-way binding = ngModel [OK]
- Confusing one-way binding with two-way binding
- Thinking ngModel only updates component or template
- Mixing ngModel with event binding
username using ngModel?Solution
Step 1: Recall ngModel syntax
The correct syntax for two-way binding uses square and round brackets together:[(ngModel)].Step 2: Evaluate options
uses[(ngModel)]="username", which is correct. Others use only one bracket or no brackets, which is invalid for two-way binding.Final Answer:
<input [(ngModel)]="username"> -> Option AQuick Check:
Two-way binding syntax = [(ngModel)] [OK]
- Using only [ngModel] or (ngModel) instead of [(ngModel)]
- Forgetting to add FormsModule import
- Writing ngModel without brackets
export class AppComponent {
name = 'Alice';
}And this template snippet:
<input [(ngModel)]="name">
<p>Hello, {{name}}!</p>What will be displayed if the user changes the input text to "Bob"?
Solution
Step 1: Understand two-way binding effect
The[(ngModel)]="name"binds input and component propertynameboth ways, so changing input updatesname.Step 2: Predict displayed output
When user types "Bob",namebecomes "Bob", so{{name}}shows "Bob" in the paragraph.Final Answer:
Hello, Bob! -> Option CQuick Check:
Input change updates name = Hello, Bob! [OK]
- Thinking interpolation {{name}} won't update
- Assuming input change doesn't affect component property
- Confusing one-way binding with two-way binding
<input (ngModel)="email">
Solution
Step 1: Identify ngModel syntax error
The snippet uses only parentheses (event binding) instead of two-way binding syntax[(ngModel)].Step 2: Check other options
ngModel is valid on input, FormsModule import is not shown but not guaranteed error here, and curly braces are not used for binding syntax.Final Answer:
Missing square brackets for two-way binding syntax -> Option BQuick Check:
Two-way binding needs [( )] brackets [OK]
- Using (ngModel) instead of [(ngModel)]
- Forgetting to import FormsModule
- Confusing event binding with two-way binding
selectedColor using ngModel. Which code snippet correctly implements two-way binding and updates the property when the user changes the selection?Solution
Step 1: Understand two-way binding on select
Two-way binding requires[(ngModel)]syntax to sync the selected value with the component property.Step 2: Evaluate options
uses[(ngModel)]="selectedColor"with proper option values, enabling two-way binding. uses one-way binding only. uses event binding but misses the input binding part. uses invalid syntax without brackets.Final Answer:
Option A code snippet with [(ngModel)] on select -> Option AQuick Check:
Select two-way binding = [(ngModel)] [OK]
- Using only [ngModel] or (ngModelChange) without two-way binding
- Missing value attributes on option tags
- Writing ngModel without brackets
Ready to prove it?
Pass the test and this pattern moves unknown
