Skip to main content

Posts

Showing posts from March, 2026

Reactive Forms & Form Validation

Reactive Forms in Angular Reactive Forms in Angular are model-driven , giving you programmatic control over form logic, structure, and validation entirely from the component class. They're great for complex, dynamic forms and large applications. 🔧 Setup Import ReactiveFormsModule in AppModule : import { ReactiveFormsModule } from '@angular/forms'; @NgModule({ imports: [ReactiveFormsModule] }) export class AppModule { } 🧠 Example: Reactive Form with Validation 📄 Component Class ( app.component.ts ) import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent { userForm: FormGroup; constructor(private fb: FormBuilder) { this.userForm = this.fb.group({ name: ['', [Validators.required]], email: ['', [Validators.required, Validators....

Template driven form

Template-Driven Forms in Angular Template-Driven Forms in Angular provide a simple, declarative way to build forms using Angular directives in the HTML template. They're perfect for basic forms , quick prototypes, or when you want to avoid a lot of TypeScript code. ✅ Key Characteristics Defined in HTML using directives like ngModel , ngForm , ngSubmit Minimal TypeScript logic Powered by FormsModule 📦 Prerequisite Import FormsModule in your AppModule . import { FormsModule } from '@angular/forms'; @NgModule({ imports: [FormsModule] }) export class AppModule { } 🧱 Basic Example 📄 Template ( app.component.html ) <form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)"> <label>Name: <input type="text" name="name" ngModel required /> </label> <br /> <label>Email: <input type="email" name="email" ngModel required /> </l...