Skip to main content

What does the ngForm directive do?

ngForm is a directive that Angular automatically adds to any <form> tag when the form uses the template-driven approach.

It:

  • creates an NgForm object that contains a FormGroup;
  • tracks the state of the whole form: valid, invalid, touched, dirty, and so on;
  • collects all fields with ngModel and binds them into a single structure;
  • allows the form to be accessed from the template (for example, via #form="ngForm").

Example:

html
<form #f="ngForm" (ngSubmit)="onSubmit(f)"> <input name="email" ngModel required> </form>

Here f is the NgForm object, which has access to the values (f.value) and status (f.valid) of the whole form.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.