Performance: ngModel for form binding
This affects the interaction responsiveness and rendering speed when binding form inputs to component data.
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-good-form', standalone: true, template: `<input [value]="name()" (input)="name.set($any($event.target).value)">` }) export class GoodFormComponent { name = signal(''); }
import { Component } from '@angular/core'; @Component({ selector: 'app-bad-form', template: `<input [(ngModel)]="name">` }) export class BadFormComponent { name = ''; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| ngModel two-way binding on many inputs | High (many bindings) | Multiple per keystroke | High due to frequent updates | [X] Bad |
| One-way binding with signals and event handlers | Low (controlled updates) | Single per input event | Lower paint cost | [OK] Good |
ngModel do in Angular forms?ngModel binds the input field and component property so changes in one reflect in the other.username using ngModel?[( )] around ngModel.[(ngModel)]="username", which binds input and property both ways.export class MyComponent {
name = 'Alice';
}<input [(ngModel)]="name">
<p>Hello, {{name}}!</p>name because of [(ngModel)].<input [(ngModel)]="email">
export class UserComponent {
email: string;
}ngModel requires importing FormsModule in the module where this component is declared.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?[(ngModel)] on both inputs.Pass the test and this pattern moves unknown