Performance: ngForm directive and form state
MEDIUM IMPACT
This affects how Angular tracks form input changes and validation, impacting interaction responsiveness and rendering updates.
<form ngForm #formRef="ngForm" (ngSubmit)="onSubmit(formRef)"> <input name="email" ngModel /> <input name="password" ngModel /> <button type="submit">Submit</button> </form>
<form #formRef="ngForm"> <input name="email" [(ngModel)]="email" (ngModelChange)="onChange()" /> <input name="password" [(ngModel)]="password" (ngModelChange)="onChange()" /> </form>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using (ngModelChange) on every input | Many event handlers attached | Multiple reflows per keystroke | Higher paint cost due to frequent updates | [X] Bad |
| Using ngForm with ngModel and submit event | Minimal event handlers | Single reflow on submit | Lower paint cost with batched updates | [OK] Good |