The ngForm directive helps Angular track and manage the state of a form easily. It tells Angular to watch the form's inputs and know if the form is valid, dirty, or touched.
ngForm directive and form state in Angular
<form #formName="ngForm">
<!-- form controls here -->
</form>The #formName="ngForm" creates a local variable to access the form's state.
You can use this variable to check properties like formName.valid, formName.dirty, and formName.touched.
<form #myForm="ngForm"> <input name="email" ngModel required email /> <button [disabled]="!myForm.valid">Submit</button> </form>
<form #loginForm="ngForm"> <input name="username" ngModel required /> <input name="password" ngModel required type="password" /> <p *ngIf="loginForm.dirty && loginForm.invalid">Please fill all fields correctly.</p> <button type="submit">Login</button> </form>
This component uses ngForm to track the form state. The submit button is disabled until the form is valid. When submitted, it shows the entered data and resets the form.
import { Component } from '@angular/core'; @Component({ selector: 'app-simple-form', template: ` <form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)"> <label for="name">Name:</label> <input id="name" name="name" ngModel required /> <label for="age">Age:</label> <input id="age" name="age" ngModel type="number" required min="1" /> <button type="submit" [disabled]="!userForm.valid">Submit</button> </form> <p *ngIf="submitted"> Form Submitted! Name: {{ submittedData.name }}, Age: {{ submittedData.age }} </p> ` }) export class SimpleFormComponent { submitted = false; submittedData = { name: '', age: '' }; onSubmit(form: any) { if (form.valid) { this.submittedData = form.value; this.submitted = true; form.resetForm(); } } }
The ngForm directive automatically creates a FormGroup behind the scenes.
Use formName.resetForm() to clear the form and reset its state.
Form controls must have a name attribute for ngForm to track them.
ngForm helps Angular track form inputs and their states.
Use the local form variable to check if the form is valid, dirty, or touched.
It makes managing form validation and submission easier and cleaner.
Practice
ngForm directive do in Angular?Solution
Step 1: Understand the role of
ThengFormngFormdirective is used to track the state of a form and its inputs in Angular.Step 2: Identify what
It does not style elements, replace HTML tags, or send data directly; it manages form state and validation.ngFormdoes not doFinal Answer:
It tracks the state and validity of a form and its inputs. -> Option AQuick Check:
ngForm tracks form state = D [OK]
- Thinking ngForm styles the form
- Assuming ngForm replaces HTML form tags
- Believing ngForm sends data automatically
ngForm in Angular?Solution
Step 1: Recall the syntax for local form reference
To access the form's state, Angular uses a template reference variable withngFormas its value, like#myForm="ngForm".Step 2: Check each option's correctness
... correctly uses#myForm="ngForm". ... misses the directive name, C misplaces the directive, and D uses incorrect binding syntax.Final Answer:
<form #myForm="ngForm"> ... </form> -> Option BQuick Check:
Local form reference = #myForm="ngForm" [OK]
- Omitting the "ngForm" in the reference
- Using square brackets incorrectly
- Placing ngForm attribute wrongly
<form #f="ngForm"> <input name="email" ngModel required> <button [disabled]="!f.form.valid">Submit</button> </form>
What happens to the Submit button when the input is empty?
Solution
Step 1: Understand form validity with required input
The input has therequiredattribute, so if empty, the form is invalid.Step 2: Analyze the button's disabled binding
The button is disabled when!f.form.validis true, meaning when the form is invalid.Final Answer:
The Submit button is disabled because the form is invalid. -> Option CQuick Check:
Required empty input disables button = B [OK]
- Thinking button enables on untouched input
- Confusing dirty and valid states
- Ignoring required validation effect
<form #myForm="ngForm"> <input name="username" ngModel> <button [disabled]="myForm.invalid">Submit</button> </form>
Why does the Submit button remain enabled even when the input is empty?
Solution
Step 1: Check input validation rules
The input has no validation likerequired, so it does not make the form invalid when empty.Step 2: Confirm form validity property usage
myForm.invalidis valid shorthand formyForm.form.invalid, so no error there.Final Answer:
The input lacks a validation rule like required, so form is valid. -> Option DQuick Check:
Missing required means form valid = C [OK]
- Assuming myForm.invalid is incorrect syntax
- Forgetting to add validation rules
- Thinking ngModel is missing when it is present
ngForm states to achieve this?Solution
Step 1: Understand form state properties
myForm.form.invalidis true if form is invalid;myForm.form.dirtyis true if user changed any input.Step 2: Match condition to requirement
We want to disable the button only if form is invalid AND user has changed input, so both must be true.Final Answer:
[disabled]="myForm.form.invalid && myForm.form.dirty" -> Option AQuick Check:
Disable if invalid and dirty = A [OK]
- Using OR instead of AND for disabling
- Confusing touched with dirty
- Checking valid instead of invalid
Ready to prove it?
Pass the test and this pattern moves unknown
