ngModel helps connect form inputs to your component data easily. It keeps the input and data in sync automatically.
ngModel for form binding in Angular
<input [(ngModel)]="propertyName" />The [(ngModel)] syntax is called two-way binding. It means the input updates the property and the property updates the input.
You must import FormsModule in your Angular module to use ngModel.
username property in the component.<input [(ngModel)]="username" />email property inside a user object.<input [(ngModel)]="user.email" />age property.<input [(ngModel)]="age" type="number" />
This component has an input bound to the name property using ngModel. As you type, the greeting updates automatically.
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-user-form', template: ` <label for="name">Name:</label> <input id="name" [(ngModel)]="name" aria-label="User name input" /> <p>Hello, {{ name }}!</p> ` }) export class UserFormComponent { name = ''; }
Always add aria-label or use <label> for accessibility.
ngModel works best for simple forms. For complex forms, consider Reactive Forms.
Remember to import FormsModule in your module to enable ngModel.
ngModel creates two-way binding between form inputs and component data.
It updates the component property when the user types and updates the input if the property changes.
Use it for simple, quick form bindings with minimal code.
Practice
ngModel do in Angular forms?Solution
Step 1: Understand ngModel purpose
ngModelbinds the input field and component property so changes in one reflect in the other.Step 2: Identify two-way binding behavior
This means typing in the input updates the property, and changing the property updates the input.Final Answer:
It creates two-way binding between the input and component property. -> Option CQuick Check:
ngModel = two-way binding [OK]
- Thinking ngModel only updates input or only updates property
- Confusing ngModel with form validation
- Assuming ngModel works without importing FormsModule
username using ngModel?Solution
Step 1: Recall Angular two-way binding syntax
Two-way binding uses the banana-in-a-box syntax:[( )]aroundngModel.Step 2: Match syntax to options
Only uses[(ngModel)]="username", which binds input and property both ways.Final Answer:
<input [(ngModel)]="username"> -> Option AQuick Check:
Two-way binding = [(ngModel)] [OK]
- Using [ngModel] which is one-way binding
- Writing ngModel without brackets or parentheses
- Using incorrect attribute like bind-ngModel
export class MyComponent {
name = 'Alice';
}And this template:
<input [(ngModel)]="name">
<p>Hello, {{name}}!</p>What will be displayed after the user types "Bob" in the input?
Solution
Step 1: Understand two-way binding effect
Typing "Bob" updates the component propertynamebecause of[(ngModel)].Step 2: Check template interpolation
The paragraph uses {{name}}, so it shows the updated value "Bob".Final Answer:
Hello, Bob! -> Option DQuick Check:
Typing updates property and view = Hello, Bob! [OK]
- Thinking interpolation won't update after typing
- Confusing initial value with updated value
- Ignoring two-way binding effect
<input [(ngModel)]="email">
Component:
export class UserComponent {
email: string;
}Solution
Step 1: Check Angular module setup
UsingngModelrequires importingFormsModulein the module where this component is declared.Step 2: Verify other code parts
The property can be uninitialized (undefined) initially, and syntax is correct. ngModel works on input elements.Final Answer:
Missing import of FormsModule in the Angular module. -> Option BQuick Check:
FormsModule import required for ngModel [OK]
- Forgetting to import FormsModule
- Thinking property must have initial value
- Believing ngModel syntax is wrong
firstName and lastName properties. You also want to show the full name live as the user types. Which template code correctly uses ngModel for this?Solution
Step 1: Use two-way binding for inputs
To update properties as user types, use[(ngModel)]on both inputs.Step 2: Display live full name with interpolation
Use {{firstName}} and {{lastName}} inside the paragraph to show updated values.Final Answer:
<input [(ngModel)]="firstName"> <input [(ngModel)]="lastName"> <p>Full name: {{firstName}} {{lastName}}</p> -> Option AQuick Check:
Two-way binding + interpolation = <input [(ngModel)]="firstName"> <input [(ngModel)]="lastName"> <p>Full name: {{firstName}} {{lastName}}</p> [OK]
- Using one-way binding [ngModel] instead of two-way
- Not using interpolation for displaying values
- Writing property names as plain text instead of {{}}
Ready to prove it?
Pass the test and this pattern moves unknown
