0
0
Angularframework~10 mins

ngForm directive and form state in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ngForm directive and form state
User opens page with form
Angular creates <form> element
ngForm directive attaches
Form controls register with ngForm
User interacts with controls
ngForm updates form state: valid, dirty, touched
Form state available for template and logic
Angular creates a form element, attaches ngForm directive, registers controls, and tracks form state as user interacts.
Execution Sample
Angular
<form #myForm="ngForm">
  <input name="email" ngModel required>
  <button [disabled]="!myForm.valid">Submit</button>
</form>
A simple form with ngForm directive tracking validity and disabling submit if invalid.
Execution Table
StepActionForm State (valid, dirty, touched)Control State (email)Button Disabled
1Page loads, form createdvalid: false, dirty: false, touched: falsevalid: false, dirty: false, touched: falsetrue
2User focuses email inputvalid: false, dirty: false, touched: falsevalid: false, dirty: false, touched: falsetrue
3User types 'a@b.com'valid: true, dirty: true, touched: falsevalid: true, dirty: true, touched: falsefalse
4User blurs email inputvalid: true, dirty: true, touched: truevalid: true, dirty: true, touched: truefalse
5User clears email inputvalid: false, dirty: true, touched: truevalid: false, dirty: true, touched: truetrue
6User submits formvalid: false, dirty: true, touched: truevalid: false, dirty: true, touched: truetrue
💡 Form remains invalid after clearing input, submit button stays disabled.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6
myForm.validfalsefalsetruetruefalsefalse
myForm.dirtyfalsefalsetruetruetruetrue
myForm.touchedfalsefalsefalsetruetruetrue
email.validfalsefalsetruetruefalsefalse
email.dirtyfalsefalsetruetruetruetrue
email.touchedfalsefalsefalsetruetruetrue
button.disabledtruetruefalsefalsetruetrue
Key Moments - 3 Insights
Why is the form invalid at the start even though no input was touched?
At step 1, the form is invalid because the required email input is empty. The form validity depends on control validity, not user interaction yet.
What does 'dirty' mean and when does it change?
Dirty means the user changed the input value. At step 3, typing 'a@b.com' sets dirty to true. Before that, it was false.
Why does the submit button disable again after clearing the input?
At step 5, clearing the input makes the email invalid again, so form.valid becomes false and button.disabled becomes true.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the form's 'touched' state after step 3?
Atrue
Bundefined
Cfalse
Dnull
💡 Hint
Check the 'Form State' column at step 3 in the execution_table.
At which step does the submit button become enabled?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'Button Disabled' column in the execution_table.
If the email input was never touched, what would be the form's 'dirty' state at step 6?
Atrue
Bfalse
Cnull
Dundefined
💡 Hint
Refer to the 'dirty' state changes in variable_tracker for myForm and email.
Concept Snapshot
ngForm directive attaches to <form> and tracks form state.
Controls register with ngForm and update validity, dirty, touched.
Form state updates as user interacts.
Use form.valid, form.dirty, form.touched in template.
Submit button can disable based on form.valid.
ngForm simplifies reactive form state management.
Full Transcript
When Angular loads a form with the ngForm directive, it creates a form element and attaches the directive. Each input control inside registers with ngForm. The form tracks states like valid, dirty, and touched based on user interaction. Initially, the form is invalid because required inputs are empty. When the user types a valid email, the form becomes valid and dirty. Blurring the input sets touched to true. Clearing the input again invalidates the form and disables the submit button. This process helps developers easily manage form state and control UI behavior like disabling buttons.