Using ngForm Directive and Tracking Form State in Angular
📖 Scenario: You are building a simple user registration form for a website. The form will collect a user's name and email. You want to use Angular's ngForm directive to manage the form state and track if the form is valid or not.
🎯 Goal: Create an Angular standalone component with a form using the ngForm directive. The form should have two input fields: name and email. You will track the form's validity and display a message when the form is valid.
📋 What You'll Learn
Create a form using the ngForm directive
Add two input fields with names name and email
Add a variable to hold the form reference
Use Angular template syntax to check the form's validity
Display a message when the form is valid
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites and apps. Using ngForm helps manage form data and validation easily in Angular.
💼 Career
Understanding ngForm and form state is essential for Angular developers building user input forms with validation and accessibility.
Progress0 / 4 steps
1
Create the form with ngForm directive and input fields
Create a standalone Angular component named UserFormComponent. Inside its template, create a <form> element with the ngForm directive. Add two input fields inside the form: one with name="name" and another with name="email". Use type="text" for both inputs.
Angular
Hint
Remember to import FormsModule and add ngForm to the <form> tag.
2
Add a template reference variable to the form
Inside the <form> element, add a template reference variable named userForm that refers to the form's ngForm directive instance.
Angular
Hint
Use #userForm="ngForm" inside the <form> tag to create the reference.
3
Add validation to input fields and check form validity
Add the required attribute to both input fields. Below the form, add a paragraph that uses Angular's interpolation to display the text "Form is valid!" only when the form referenced by userForm is valid.
Angular
Hint
Use required on inputs and *ngIf="userForm.valid" on the paragraph.
4
Add accessibility and final touches
Add aria-label attributes to both input fields with values "Name input" and "Email input" respectively. Also, add a submit button inside the form with the text Submit.
Angular
Hint
Use aria-label on inputs and add a <button type="submit"> inside the form.
Practice
(1/5)
1. What does the ngForm directive do in Angular?
easy
A. It tracks the state and validity of a form and its inputs.
B. It styles the form elements automatically.
C. It replaces the need for HTML form tags.
D. It sends form data directly to the server.
Solution
Step 1: Understand the role of ngForm
The ngForm directive is used to track the state of a form and its inputs in Angular.
Step 2: Identify what ngForm does not do
It does not style elements, replace HTML tags, or send data directly; it manages form state and validation.
Final Answer:
It tracks the state and validity of a form and its inputs. -> Option A
Quick Check:
ngForm tracks form state = D [OK]
Hint: Remember: ngForm manages form state, not styling or data sending [OK]
Common Mistakes:
Thinking ngForm styles the form
Assuming ngForm replaces HTML form tags
Believing ngForm sends data automatically
2. Which of the following is the correct way to create a local reference to a form using ngForm in Angular?
easy
A. ...
B. ...
C. ...
D. ...
Solution
Step 1: Recall the syntax for local form reference
To access the form's state, Angular uses a template reference variable with ngForm as 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 B
Quick Check:
Local form reference = #myForm="ngForm" [OK]
Hint: Use #var="ngForm" to access form state locally [OK]
Why does the Submit button remain enabled even when the input is empty?
medium
A. The button binding should use myForm.form.invalid instead of myForm.invalid.
B. The local reference #myForm is incorrectly declared.
C. The ngModel directive is missing on the input.
D. The input lacks a validation rule like required, so form is valid.
Solution
Step 1: Check input validation rules
The input has no validation like required, so it does not make the form invalid when empty.
Step 2: Confirm form validity property usage
myForm.invalid is valid shorthand for myForm.form.invalid, so no error there.
Final Answer:
The input lacks a validation rule like required, so form is valid. -> Option D
Quick Check:
Missing required means form valid = C [OK]
Hint: Add required to input to make form invalid when empty [OK]
Common Mistakes:
Assuming myForm.invalid is incorrect syntax
Forgetting to add validation rules
Thinking ngModel is missing when it is present
5. You want to disable the Submit button only after the user has changed any input in the form and the form is invalid. Which condition correctly uses ngForm states to achieve this?
hard
A. [disabled]="myForm.form.invalid && myForm.form.dirty"
B. [disabled]="myForm.invalid || myForm.pristine"
C. [disabled]="!myForm.form.valid && myForm.touched"
D. [disabled]="myForm.form.valid && myForm.dirty"
Solution
Step 1: Understand form state properties
myForm.form.invalid is true if form is invalid; myForm.form.dirty is 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 A
Quick Check:
Disable if invalid and dirty = A [OK]
Hint: Use invalid AND dirty to disable after user changes invalid form [OK]
Common Mistakes:
Using OR instead of AND for disabling
Confusing touched with dirty
Checking valid instead of invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.