Course Progress 63%

Angular Forms – Introduction

Angular Forms Angular applications का एक core हिस्सा हैं। Forms का use user से data collect करने, validate करने और backend या component logic तक safely पहुँचाने के लिए किया जाता है। Angular forms simple HTML forms से काफी आगे हैं और structured, scalable और testable approach provide करते हैं।

इस chapter में हम Angular forms की foundation समझेंगे, forms के types क्या हैं, Angular ने forms के लिए अलग system क्यों बनाया, और आगे आने वाले chapters में क्या-क्या deep concepts cover होंगे।

Angular Forms क्या हैं

Angular Forms एक framework-level solution है जो:

  • Form inputs को component state से connect करता है
  • Validation को centralized तरीके से handle करता है
  • User interaction (touch, dirty, valid) track करता है
  • Forms को reactive और predictable बनाता है

Angular forms plain HTML forms की limitations को solve करते हैं।

HTML Forms की Limitations

Normal HTML forms में कुछ problems होती हैं:

  • Validation scattered होती है
  • Form state track करना मुश्किल होता है
  • Dynamic fields manage करना complex होता है
  • Test करना difficult होता है

Angular forms इन issues को systematically solve करते हैं।

Angular Forms के Types

Angular forms दो main approaches provide करता है:

  • Template-Driven Forms
  • Reactive Forms

दोनों approaches same goal serve करते हैं, लेकिन philosophy और usage अलग है।

Template-Driven Forms Overview

Template-Driven Forms में:

  • Form logic mostly template में लिखा जाता है
  • Directives जैसे ngModel use होते हैं
  • Small और simple forms के लिए suitable होते हैं

Example:

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

यह approach beginners के लिए easy होती है।

Reactive Forms Overview

Reactive Forms में:

  • Form structure component class में define होती है
  • Explicit form model बनाया जाता है
  • Large और complex forms के लिए best होती है

Example:

this.form = new FormGroup({
  name: new FormControl('')
});

Reactive forms ज्यादा scalable और testable होती हैं।

Template-Driven vs Reactive Forms

High-level comparison:

  • Template-Driven → simple, less code, implicit
  • Reactive → explicit, powerful, predictable

Angular official recommendation complex apps के लिए Reactive Forms को prefer करती है।

Forms Module और Setup

Angular forms use करने के लिए specific modules enable करने पड़ते हैं।

Template-Driven Forms के लिए

import { FormsModule } from '@angular/forms';

@Component({
  standalone: true,
  imports: [FormsModule],
  template: `...`
})
export class AppComponent {}

Reactive Forms के लिए

import { ReactiveFormsModule } from '@angular/forms';

@Component({
  standalone: true,
  imports: [ReactiveFormsModule],
  template: `...`
})
export class AppComponent {}

Standalone Angular में modules को component के imports में add किया जाता है।

Form Control क्या होता है

Form Control एक single input field को represent करता है।

Angular internally हर input के लिए state track करता है जैसे:

  • value
  • valid / invalid
  • touched / untouched
  • dirty / pristine

यह state Angular forms की backbone है।

Form Group क्या होता है

Form Group multiple form controls का group होता है।

Example:

  • Registration form
  • Login form
  • Profile form

यह form structure को logical तरीके से organize करता है।

Form State और User Interaction

Angular forms automatically user interaction track करता है।

Common states:

  • touched → input focus हुआ और छोड़ा गया
  • dirty → value change हुई
  • valid → सभी validations pass
  • invalid → कोई validation fail

इन states के आधार पर UI feedback दिया जाता है।

Validation का Concept

Angular forms में validation built-in होती है।

Validation का मतलब है:

  • Required fields check करना
  • Email format verify करना
  • Min / Max length enforce करना

Angular forms synchronous और asynchronous दोनों validations support करते हैं।

Two-Way Binding और Forms

Template-driven forms में two-way binding common है।

<input [(ngModel)]="email">

Reactive forms में two-way binding नहीं होती, बल्कि explicit value management होता है।

यह design decision forms को predictable बनाता है।

Forms और Change Detection

Angular forms change detection के साथ deeply integrated होते हैं।

  • Input change → form state update
  • Validation change → UI update
  • Submit → component logic trigger

यह सब automatically handle होता है।

Angular Forms क्यों जरूरी हैं

Angular forms benefits:

  • Centralized form logic
  • Consistent validation
  • Clean data flow
  • Easy testing
  • Better UX

Forms Angular applications में सिर्फ input fields नहीं, बल्कि business logic का entry point होते हैं।

आगे क्या सीखेंगे

अगले chapters में हम:

  • Template-Driven Forms deep dive
  • Reactive Forms complete coverage
  • Validation techniques
  • Custom validators
  • Dynamic forms

Angular Forms Introduction आपको foundation देता है, जिस पर आगे के advanced form concepts build होते हैं।