Sub topic 4
The form as a whole: NgForm and #f="ngForm"
This chapter assumes you know Angular components, template reference variables, and how [(ngModel)] binds a single input. It covers the object that represents the whole form: NgForm.
Where NgForm comes from
You never write NgForm in a template. When a component imports FormsModule, Angular matches the directive selector form:not([ngNoForm]):not([formGroup]) against every <form> element. Each match creates an NgForm instance on that element.
NgForm owns a top-level FormGroup, the same class reactive forms use. Each ngModel input inside the form registers its own FormControl with that group. The name attribute is the key it registers under.
Getting a handle with #f="ngForm"
A template reference variable on an element normally points to the DOM element. #f alone gives you an HTMLFormElement. Writing #f="ngForm" asks Angular for the directive whose exportAs name is ngForm, so f becomes the NgForm instance.
import { Component } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { FormsModule, NgForm } from '@angular/forms';
@Component({
selector: 'app-profile',
imports: [FormsModule, JsonPipe],
template: `
<form #f="ngForm">
<input name="email" [(ngModel)]="model.email" required email />
<input name="city" [(ngModel)]="model.city" />
<p>Valid: {{ f.valid }}</p>
<p>Value: {{ f.value | json }}</p>
<p>Controls: {{ controlNames(f) }}</p>
</form>
`,
})
export class ProfileComponent {
model = { email: '', city: '' };
controlNames(form: NgForm): string {
return Object.keys(form.controls).join(', ');
}
}The properties you will use most often:
| Property | Meaning |
|---|---|
| `f.value` | Plain object of all control values, keyed by `name` |
| `f.valid` / `f.invalid` | `true` only if every control passes validation |
| `f.controls` | Map of `name` to `NgModel`'s `FormControl` |
| `f.submitted` | `true` after the first submit event |
| `f.form` | The underlying `FormGroup`, if you need its API |
f is also a form-level summary of state. f.invalid is true as soon as one child control is invalid, which makes it a good source for disabling a submit button.
Scope of the variable
A template reference variable is visible to the whole template, including siblings of the form. Inside @if or @for blocks, it is visible only to content in the same block or a nested one, and not to code outside. Declare #f on the <form> and read it from elements inside or beside the form in the same view.
Common errors
- Missing
nameattribute. AnngModelinside a form withoutnamethrows a "name attribute must be set" error. Either addname, or set[ngModelOptions]="{ standalone: true }"to keep the control out of the form. #fwithout="ngForm".f.validis thenundefined, becausefis the DOM element. No error appears, which makes this hard to spot.- Empty
f.valueon first render. Controls register asynchronously, sof.valueis briefly{}before the inputs join the group. Don't read it in the constructor or inngOnInit. - Native validation interfering. Angular adds
novalidateto the form automatically. You don't need to add it yourself.
Exercise
A teammate writes <form #f> and <button [disabled]="f.invalid">. The button is never disabled, even with empty required fields, and there is no error. What is wrong and how do you fix it?
Show answer
#f without a value refers to the HTMLFormElement, which has no invalid property, so f.invalid is undefined (falsy). Change it to <form #f="ngForm"> so f is the NgForm directive instance.