Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

Angular Forms

Learn how Angular forms collect user input, track form state, validate data and submit information in an application.

What are Angular Forms?

Forms are used when an application needs to collect information from users.

Examples include course enquiries, registration forms, login forms, contact forms and application forms.

Angular provides two major approaches for working with forms: Template-driven Forms and Reactive Forms.

Angular Forms Flow

User Enters Data
→
Angular Form Tracks Input
→
Validation Checks Data
→
Submit Process Data

Main Angular Form Concepts

1
Form Controls

Represent individual fields such as name, email or course.

2
Form State

Angular tracks whether a form is valid, dirty, touched or submitted.

3
Validation

Validation checks whether entered values satisfy requirements.

4
Submission

Valid form data can be processed when the user submits the form.

Two Approaches to Angular Forms

Template-driven Form logic is primarily defined in the template.
VS
Reactive Forms Form structure and logic are defined in TypeScript.

Template-driven Forms

Template-driven forms use Angular directives inside the HTML template to create and manage the form.

They are useful for simple forms where most of the form behavior can be described directly in the template.

Template Example
<form #enquiryForm="ngForm">

    <input
        type="text"
        name="name"
        ngModel>

    <input
        type="email"
        name="email"
        ngModel>

    <button type="submit">
        Submit
    </button>

</form>

Reactive Forms

Reactive forms create the form model in TypeScript using objects such as FormControl and FormGroup.

They provide a programmatic approach that is useful for complex forms and dynamic validation requirements.

Component Example
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
    selector: 'app-enquiry',
    templateUrl: './enquiry.component.html'
})
export class EnquiryComponent {

    enquiryForm = new FormGroup({

        name: new FormControl(''),

        email: new FormControl(''),

        course: new FormControl('')

    });

}
Template
<form [formGroup]="enquiryForm">

    <input
        type="text"
        formControlName="name">

    <input
        type="email"
        formControlName="email">

    <input
        type="text"
        formControlName="course">

</form>

FormControl

FormControl represents a single form field and keeps track of its current value and state.

For example, an email field can be represented by one FormControl.

Example
email = new FormControl('');
Reading Value
console.log(
    this.email.value
);

FormGroup

FormGroup groups multiple form controls into a single form model.

It allows the application to manage related fields together.

Example
registrationForm = new FormGroup({

    name: new FormControl(''),

    email: new FormControl(''),

    mobile: new FormControl(''),

    course: new FormControl('')

});

CIIT Training Institute Example

Suppose CIIT Training Institute has an online enquiry form where visitors enter their name, email and preferred course.

Angular can collect this information and validate it before sending the data to the backend.

Enquiry Form
<form>

    <label>
        Name
    </label>

    <input
        type="text"
        placeholder="Enter your name">


    <label>
        Email
    </label>

    <input
        type="email"
        placeholder="Enter your email">


    <label>
        Course
    </label>

    <select>

        <option>
            Java Full Stack
        </option>

        <option>
            .NET Full Stack
        </option>

        <option>
            Python Full Stack
        </option>

    </select>


    <button type="submit">
        Send Enquiry
    </button>

</form>

CIIT Training Institute Enquiry

Name: Enter your name

Email: Enter your email

Course: Select Course

Form State

Angular tracks the state of form controls and forms. This information can be used to decide when to show validation messages or enable actions.

Common states include valid, invalid, touched, untouched, dirty and pristine.

V
Valid

Field or form satisfies its validation rules.

I
Invalid

One or more validation rules are not satisfied.

T
Touched

The user has interacted with the field and left it.

D
Dirty

The field value has been changed by the user.

Form Validation

Validation ensures that the entered data satisfies the application's requirements before the form is processed.

Angular provides built-in validators for common requirements such as required fields and email values.

Reactive Form Validation
import { Validators } from '@angular/forms';

email = new FormControl(
    '',
    [
        Validators.required,
        Validators.email
    ]
);

Displaying Validation Message

Template
<input
    type="email"
    [formControl]="email">

<div
    *ngIf="email.invalid && email.touched">

    Please enter a valid email address.

</div>

Form Submission

When the user submits a form, Angular can collect the current form values and send them for further processing.

The application should normally validate the form before processing or sending its data.

Submit Method
submitForm() {

    if (this.enquiryForm.valid) {

        console.log(
            this.enquiryForm.value
        );

    }

}

Template-driven vs Reactive Forms

Feature Template-driven Reactive
Form Definition Mainly in template Mainly in TypeScript
Form Model Created through directives Explicitly created
Complexity Good for simpler forms Good for complex forms
Validation Template-based validators Programmatic validators
Testing More template-oriented Form model is easier to test independently

Common Uses of Angular Forms

L
Login

Collect username or email and password.

E
Enquiry

Collect course and contact information.

R
Registration

Capture user registration information.

C
Contact

Collect messages and contact details.

Important Points

1. Choose the Right Form Approach

Use template-driven forms for simpler scenarios and reactive forms when more control is required.

2. Validate User Input

Validate important fields before processing form data.

3. Track Form State

Use form state such as valid, invalid, touched and dirty to control the user experience.

4. Keep Forms Easy to Understand

Use meaningful field names and clear validation messages.

Angular Forms Best Practices

  • Use meaningful names for controls and form fields.
  • Validate user input before processing form data.
  • Display clear and useful validation messages.
  • Keep complex form logic inside the component rather than putting unnecessary logic into the template.
  • Use reactive forms when the form has complex validation or dynamic controls.
  • Do not submit invalid or incomplete data to the backend.

Summary

Angular Forms are used to collect, track, validate and submit user input.

Angular provides two main approaches: Template-driven Forms and Reactive Forms.

Forms can use controls, validation and form state to create reliable user-input workflows. In the next topic, we will learn about Template-driven Forms.