Sub topic 3
Binding inputs with ngModel
This chapter assumes you know TypeScript classes, Angular components and templates, property and event binding, and basic HTML form elements. It also assumes you have imported FormsModule into your component, as the previous chapter showed.
What ngModel does
ngModel is a directive from FormsModule. When you put it on an input, Angular creates a FormControl behind the scenes and connects it to that element. The control holds the value, the validation state, and the touched and dirty flags. You never construct it yourself.
There are three ways to write it:
| Syntax | Direction | Typical use |
|---|---|---|
| `ngModel` | None, only registers the control | Reading values from `f.value` on submit |
| `[ngModel]="x"` | Component to view | Showing a value, handling changes yourself |
| `[(ngModel)]="x"` | Both directions | Keeping a model object in sync with the form |
The two-way form [(ngModel)] is shorthand for [ngModel]="x" (ngModelChange)="x = $event". Use the long form when you need to transform the value before storing it.
A minimal example
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-profile',
imports: [FormsModule],
template: `
<form>
<label for="name">Name</label>
<input id="name" name="name" [(ngModel)]="model.name" />
<label for="email">Email</label>
<input id="email" name="email" type="email" [(ngModel)]="model.email" />
</form>
<p>Hello, {{ model.name }}</p>
<pre>{{ model | json }}</pre>
`,
})
export class ProfileComponent {
model = { name: '', email: '' };
}Typing in either input updates model immediately, and the paragraph and the JSON dump update with it. Changing model.name in code updates the input too.
The name attribute is required
Inside a <form>, every control that uses ngModel must have a name attribute. Angular uses it as the key when it registers the control with the parent NgForm. It is also the key you see in f.value.
If you omit it, Angular throws an error at runtime:
NG01352: If ngModel is used within a form tag, either the name attribute
must be set or the form control must be defined as 'standalone' in ngModelOptions.The name does not have to match the model property. In the example above, name="name" and model.name happen to agree, but name="fullName" with [(ngModel)]="model.name" works the same way. The name controls the key in f.value. The expression controls where the data lives in your component.
Standalone controls
Sometimes an input sits inside a form but should not be part of it, such as a search box in a page-wide form. Mark it as standalone so it needs no name and does not appear in f.value:
<input [(ngModel)]="query" [ngModelOptions]="{ standalone: true }" />How the data flows
The second half of this flow is asynchronous. When you change the model in code, ngModel writes the new value to the control in a resolved promise, not immediately. Reading form.value or control.value in the same tick as your assignment can return the old value.
Common errors
- Missing
nameinside a form. Gives the NG01352 error above. Addname, or setstandalone: true. - Forgetting to import
FormsModule. The template fails with "Can't bind to 'ngModel' since it isn't a known property of 'input'". AddFormsModuleto the component'simports. - Binding to a getter without a setter.
[(ngModel)]must write back to its target, so it needs an assignable property. - Reading the value right after setting it. See the asynchronous note above.
Exercise
An input inside a <form> has [(ngModel)]="user.email" but no name attribute. What happens, and what are two ways to fix it?
Show answer
Angular throws an error (NG01352) because it cannot register the control with the parent NgForm. Fix it by adding a name attribute, for example name="email", or by adding [ngModelOptions]="{ standalone: true }". The second option keeps the control out of the form's value.
Exercise
What is [(ngModel)]="model.name" shorthand for, and when would you write the long form?
Show answer
It is shorthand for [ngModel]="model.name" (ngModelChange)="model.name = $event". Use the long form when you need to transform or validate the value before storing it, or run extra logic on each change.