What are Angular Forms?
1. What are Angular Forms?
Angular Forms are Angular's mechanism for collecting, validating, tracking, and submitting user input.
Forms are commonly used for:
- Login
- Registration
- Contact forms
- Product/customer entry
- Search/filter forms
- Profile editing
- Admin CRUD screens
For example:
Name: [ Syed ]
Email: [ syed@example.com ]
Password: [ ******** ]
[ Submit ]
Angular helps us manage not only the values entered into these fields, but also their validation and state.
2. Types of Forms in Angular
Angular provides two main approaches:
| Feature | Template-Driven Forms | Reactive Forms |
|---|---|---|
| Main logic | HTML template | TypeScript |
| Uses | ngModel |
FormControl, FormGroup |
| Module | FormsModule |
ReactiveFormsModule |
| Best suited for | Simple forms | Complex forms |
| Validation | Mostly template | Mostly TypeScript |
| Testing | Less convenient | Easier |
| Dynamic forms | More difficult | Easier |
Template-Driven Form
The form is mainly controlled through the HTML template.
<input [(ngModel)]="name" name="name">
Reactive Form
The form structure and validation are mainly controlled from TypeScript.
name = new FormControl('');
For large business applications, Reactive Forms are commonly preferred because the form model and validation rules are explicit in TypeScript.
3. Full Simple Sample Program
Let's first see a simple Template-Driven Form.
app.ts
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-root',
standalone: true,
imports: [FormsModule],
templateUrl: './app.html'
})
export class App {
name = '';
email = '';
submitForm() {
console.log('Name:', this.name);
console.log('Email:', this.email);
}
}
app.html
<h2>Contact Form</h2>
<form (ngSubmit)="submitForm()">
<div>
<label>Name:</label>
<input
type="text"
name="name"
[(ngModel)]="name">
</div>
<br>
<div>
<label>Email:</label>
<input
type="email"
name="email"
[(ngModel)]="email">
</div>
<br>
<button type="submit">
Submit
</button>
</form>
<hr>
<p>Name: {{ name }}</p>
<p>Email: {{ email }}</p>
4. Program Explanation
Step 1: Import FormsModule
import { FormsModule } from '@angular/forms';
FormsModule provides functionality required for template-driven forms, including ngModel.
Because this is a standalone component, we add it directly to:
imports: [FormsModule]
Step 2: Create properties
name = '';
email = '';
These properties store the values entered by the user.
Initially:
name = ""
email = ""
Suppose the user enters:
Name → Syed
Email → syed@example.com
Angular updates the component:
name = "Syed"
email = "syed@example.com"
Step 3: Use ngModel
<input
type="text"
name="name"
[(ngModel)]="name">
[(ngModel)] performs two-way data binding.
Conceptually:
Component
↓
name = "Syed"
↓
Input textbox
and
Input textbox
↓
User types "Mohamed"
↓
name = "Mohamed"
So changes can flow in both directions.
Step 4: Submit the form
<form (ngSubmit)="submitForm()">
When the user clicks:
<button type="submit">Submit</button>
Angular calls:
submitForm() {
console.log('Name:', this.name);
console.log('Email:', this.email);
}
The output could be:
Name: Syed
Email: syed@example.com
5. What Does Angular Manage in a Form?
Angular forms do much more than simply read textbox values.
Angular can track:
Form
├── Values
├── Validation
├── Errors
└── State
├── valid / invalid
├── touched / untouched
├── dirty / pristine
└── submitted
For example, Angular can determine whether a field is:
Valid / Invalid
valid → passes validation
invalid → fails validation
Example:
<input type="email">
Touched / Untouched
untouched
↓
User enters the field
↓
User leaves the field
↓
touched
This is useful for deciding when to display validation errors.
Pristine / Dirty
pristine → user hasn't changed the value
dirty → user changed the value
6. Simple Validation Example
We can add validation:
<input
type="text"
name="name"
[(ngModel)]="name"
required
minlength="3">
Angular now knows that:
Empty
↓
INVALID
"Sy"
↓
INVALID
"Syed"
↓
VALID
So Angular forms provide both data collection and validation/state management.
7. Angular Forms Flow
A typical Angular form works like this:
User
↓
HTML Form
↓
Angular Form APIs
↓
Validation
↓
Component
↓
Service
↓
ASP.NET Core Web API
↓
Database
For example, in an Angular + ASP.NET Core application:
Registration Form
↓
Angular validates input
↓
Angular Service
↓
POST /api/users
↓
ASP.NET Core Web API
↓
SQL Server
Angular performs client-side validation, but the ASP.NET Core API must still perform server-side validation. Client-side validation alone is not a security boundary.
8. Key Points
- Angular Forms are used to capture and manage user input.
- Angular provides Template-Driven Forms and Reactive Forms.
- Template-driven forms commonly use
FormsModuleand[(ngModel)]. - Reactive forms use
ReactiveFormsModule,FormControl,FormGroup, etc. - Angular can track
valid,invalid,touched,untouched,dirty, andpristine. - Angular provides built-in validation and supports custom validation.
- Template-driven forms are convenient for relatively simple forms.
- Reactive forms provide more explicit control and are well suited to complex and dynamic forms.
- Angular validation improves UX, but backend validation is still required.
9. Interview Questions and Answers
1. What are Angular Forms?
Angular Forms provide APIs for capturing, validating, tracking, and submitting user input.
2. What are the types of forms in Angular?
There are two primary approaches:
1. Template-Driven Forms
2. Reactive Forms
3. What is a Template-Driven Form?
A Template-Driven Form defines most of the form behavior in the HTML template, commonly using ngModel.
<input [(ngModel)]="name" name="name">
4. What is a Reactive Form?
A Reactive Form defines the form model explicitly in TypeScript using APIs such as:
FormControl
FormGroup
FormArray
5. Which module is used for Template-Driven Forms?
FormsModule
6. Which module is used for Reactive Forms?
ReactiveFormsModule
7. What is ngModel?
ngModel is commonly used in template-driven forms to bind a form control to component data.
<input [(ngModel)]="name" name="name">
8. Template-Driven vs Reactive Forms?
The core difference is where the form model is primarily defined:
Template-Driven
↓
HTML-centric
Reactive
↓
TypeScript-centric
Reactive Forms generally provide more explicit control for complex validation, dynamic forms, and testing.
Interview Definition
Angular Forms provide a structured way to capture, validate, track, and submit user input. Angular supports two main approaches: Template-Driven Forms and Reactive Forms.