Angular: Example Code for ngModelOptions in Template-Driven Forms

Abhishek Wadalkar
1 min readSep 15, 2024

In Angular template-driven forms, the ngModelOptions directive provides control over when your form model is updated. Below is an example demonstrating how to use all three ngModelOptions update types—change, blur, and submit—in one form.

<form #blogform="ngForm" (ngSubmit)="onSubmit()">
<!-- Update on change -->
<label for="title">Title (update on change):</label>
<input id="title" [(ngModel)]="title" [ngModelOptions]="{updateOn: 'change'}">

<!-- Update on blur -->
<label for="description">Description (update on blur):</label>
<input id="description" [(ngModel)]="description" [ngModelOptions]="{updateOn: 'blur'}">

<!-- Update on submit -->
<label for="content">Content (update on submit):</label>
<input id="content" [(ngModel)]="content" [ngModelOptions]="{updateOn: 'submit'}">

<button type="submit">Submit</button>
</form>

Explanation:

  • Update on blur: The model updates only when the input field loses focus. This is useful when you want to delay model updates until the user finishes editing a field.
  • Update on change: The default behavior where the model updates on every keystroke. This is great for real-time validation or live updates.
  • Update on submit: The model updates only when the form is submitted, which helps delay any validation or data handling until the user is ready to submit the form.

For a full breakdown of ngModelOptions in Angular forms, including how to use the standalone option, check out the previous post here: Understanding ngModelOptions in Template-Driven Forms.

--

--

Abhishek Wadalkar
Abhishek Wadalkar

Written by Abhishek Wadalkar

Passionate Frontend developer with 4 years experience, crafting seamless, user-centric web experiences. Exploring the world of web development and constantly.

No responses yet