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 Template-driven Forms

Learn how to create Angular forms using template-driven approach, ngModel, ngForm and template reference variables.

What are Template-driven Forms?

Template-driven forms are an Angular form approach where most of the form structure and behavior is defined directly in the HTML template.

Angular uses directives such as ngModel and ngForm to create and manage the form.

This approach is generally convenient for simple forms such as basic login, contact and enquiry forms.

Template-driven Form Flow

HTML Template Define Form
→
ngModel Track Controls
→
ngForm Track Form
→
Submit Process Data

Main Concepts

F
ngForm

Represents and tracks the overall template-driven form.

M
ngModel

Connects a form control with Angular's form model.

R
Reference Variable

Provides access to form or control state inside the template.

V
Validation

Built-in validation attributes can be applied to controls.

Import FormsModule

Template-driven forms use Angular's FormsModule.

The module must be imported into the Angular application before using template-driven form features such as ngModel.

Standalone Component Example
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

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

}

Basic Template-driven Form

A template-driven form can be created by adding ngForm to the form element and ngModel to the controls.

Each control should have a unique name attribute so Angular can register it with the form.

HTML Template
<form #enquiryForm="ngForm">

    <label>
        Name
    </label>

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


    <label>
        Email
    </label>

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


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

</form>

Using ngModel

ngModel connects a form control with Angular's form state.

It can be used with the name attribute to register the control inside a template-driven form.

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

Form Control

The fullName field is registered with the Angular form.

Using ngForm

Angular automatically creates a form directive when ngForm is applied to a form.

A template reference variable can be used to access the form's state.

Example
<form
    #courseForm="ngForm">

    ...

</form>
Checking Form State
<p>
    Form Valid:
    {{ courseForm.valid }}
</p>

<p>
    Form Submitted:
    {{ courseForm.submitted }}
</p>

Two-way Binding with ngModel

Template-driven forms commonly use two-way binding with the [(ngModel)] syntax.

This allows the template control and component property to stay synchronized.

Component
course = 'Angular';
Template
<input
    type="text"
    name="course"
    [(ngModel)]="course">

<p>
    Selected Course:
    {{ course }}
</p>

CIIT Training Institute Example

Suppose CIIT Training Institute has a course enquiry form. A visitor can enter their name, email, mobile number and preferred course.

This is a good example of a simple form that can be created using the template-driven approach.

Component
export class EnquiryComponent {

    name = '';

    email = '';

    mobile = '';

    course = '';

}
Template
<form
    #enquiryForm="ngForm"
    (ngSubmit)="submitForm(enquiryForm)">


    <label>
        Full Name
    </label>

    <input
        type="text"
        name="name"
        [(ngModel)]="name"
        required>


    <label>
        Email
    </label>

    <input
        type="email"
        name="email"
        [(ngModel)]="email"
        required>


    <label>
        Mobile
    </label>

    <input
        type="text"
        name="mobile"
        [(ngModel)]="mobile"
        required>


    <label>
        Preferred Course
    </label>

    <select
        name="course"
        [(ngModel)]="course"
        required>

        <option value="">
            Select Course
        </option>

        <option value="Java Full Stack">
            Java Full Stack
        </option>

        <option value=".NET Full Stack">
            .NET Full Stack
        </option>

        <option value="Python Full Stack">
            Python Full Stack
        </option>

    </select>


    <button
        type="submit"
        [disabled]="enquiryForm.invalid">

        Send Enquiry

    </button>

</form>
Submit Method
submitForm(form: NgForm) {

    if (form.valid) {

        console.log(
            form.value
        );

    }

}

Validation in Template-driven Forms

HTML validation attributes can be used with Angular template-driven forms.

Common validation attributes include required, email, minlength and maxlength.

Required Validation
<input
    type="text"
    name="name"
    ngModel
    required>
Email Validation
<input
    type="email"
    name="email"
    ngModel
    required
    email>
Length Validation
<input
    type="text"
    name="mobile"
    ngModel
    minlength="10"
    maxlength="10"
    required>

Displaying Validation Messages

A template reference variable can be created for a control to check its validation state.

For example, a required field can display an error after the user has interacted with it.

Example
<input
    type="text"
    name="name"
    ngModel
    required
    #nameControl="ngModel">


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

    Name is required.

</div>

Template-driven Form States

V
Valid

All active validation rules are satisfied.

I
Invalid

One or more controls fail validation.

T
Touched

The user has interacted with the control.

D
Dirty

The control value has been changed.

Submitting a Template-driven Form

The ngSubmit event can be used to handle form submission.

The form should normally be checked for validity before its data is processed.

Submit Example
<form
    #courseForm="ngForm"
    (ngSubmit)="submitForm(courseForm)">

    ...

    <button
        type="submit"
        [disabled]="courseForm.invalid">

        Submit

    </button>

</form>
TypeScript
submitForm(form: NgForm) {

    if (form.valid) {

        console.log(
            'Form submitted',
            form.value
        );

    }

}

Reading Form Values

The value property of the template form contains the current values of its registered controls.

This allows the submitted information to be processed by the component.

Example
submitForm(form: NgForm) {

    console.log(
        form.value
    );

}

Resetting a Template-driven Form

Angular provides the resetForm() method to reset the form and its state.

This can be useful after successful submission or when the user wants to clear all entered information.

Example
resetForm(form: NgForm) {

    form.resetForm();

}

Important Rules

1. Use FormsModule

Import FormsModule before using template-driven form features.

2. Give Controls a Name

Registered controls should have a name attribute inside the form.

3. Use ngModel

Use ngModel to connect input controls with Angular's form state.

4. Validate Before Processing

Check the form's validity before processing or sending its values.

Advantages of Template-driven Forms

  • Simple to understand for basic forms.
  • Much of the form structure can be defined directly in HTML.
  • Requires less explicit form-model code for simple scenarios.
  • Convenient for common input fields and basic validation.
  • Works well for straightforward forms with limited dynamic behavior.

Limitations of Template-driven Forms

  • Complex forms can become difficult to manage in large templates.
  • Dynamic form structures are generally easier to handle with reactive forms.
  • Large validation requirements can make templates harder to maintain.
  • Complex form logic can become closely tied to the template.

Template-driven Form Key Features

Feature Purpose Example
ngForm Tracks the overall form #form="ngForm"
ngModel Registers and binds controls ngModel
[(ngModel)] Two-way data binding [(ngModel)]="name"
ngSubmit Handles form submission (ngSubmit)="submitForm()"
required Makes a field mandatory required

Template-driven Forms Best Practices

  • Use template-driven forms for relatively simple form scenarios.
  • Give every registered control a meaningful name.
  • Use [(ngModel)] when component and form values need two-way synchronization.
  • Show validation messages only when they are useful to the user.
  • Disable submission when the form is invalid where appropriate.
  • Move complex business logic out of the template.

Summary

Template-driven forms provide a simple way to create and manage Angular forms directly through the HTML template.

Important features include FormsModule, ngForm, ngModel, ngSubmit and template-based validation.

Template-driven forms are especially useful for straightforward forms such as basic enquiries, login and contact forms. In the next topic, we will learn about Reactive Forms.