ရိုးရိုးလေး ရှင်းပြမယ်
Template-Driven Forms ကို ရိုးရိုးလေးနားလည်မယ်
Template-Driven Forms များသည် HTML template ထဲတွင် ngModel directive ဖြင့် forms များ တည်ဆောက်သည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Error message အားလုံးကို page စပေါ်ကတည်းကပြခြင်းက user experience ဆိုးပြီး form state ကိုမသုံးနိုင်တာပြပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Template-driven forms က directives နဲ့ template declarations ကိုသုံးပြီး form model တည်ဆောက်တာကြောင့် field နည်းသောရိုးရှင်းတဲ့ forms အတွက်သင့်တော်ပါတယ်။ Name attributes, ngModel state, validation flags နဲ့ submit lifecycle ကိုနားလည်ပြီး domain model နဲ့ raw form input ကိုခွဲစဉ်းစားပါ။ Async/complex cross-field rules သို့ dynamic forms များလာလျှင် reactive forms ကပိုရှင်းနိုင်ပါတယ်။
- User input
- Binding + validation
- Valid state / feedback
Template-Driven Forms ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule]
})
// component.html
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
<div>
<label>Name:</label>
<input type="text" name="name" [(ngModel)]="user.name" required>
</div>
<div>
<label>Email:</label>
<input type="email" name="email" [(ngModel)]="user.email" required email>
</div>
<button type="submit" [disabled]="!userForm.valid">Submit</button>
</form>
// component.ts
export class MyComponent {
user = { name: '', email: '' };
onSubmit(form: NgForm) {
console.log('Form Data:', this.user);
console.log('Form Valid:', form.valid);
}
}Template-driven forms က directives နဲ့ template declarations ကိုသုံးပြီး form model တည်ဆောက်တာကြောင့် field နည်းသောရိုးရှင်းတဲ့ forms အတွက်သင့်တော်ပါတယ်။ Name attributes, ngModel state, validation flags နဲ့ submit lifecycle ကိုနားလည်ပြီး domain model နဲ့ raw form input ကိုခွဲစဉ်းစားပါ။ Async/complex cross-field rules သို့ dynamic forms များလာလျှင် reactive forms ကပိုရှင်းနိုင်ပါတယ်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- User input
// app.module.tsTemplate-Driven Forms များသည် HTML template ထဲတွင် ngModel directive ဖြင့် forms များ တည်ဆောက်သည်။
- Binding + validation
<label>Email:</label>Template-driven forms က directives နဲ့ template declarations ကိုသုံးပြီး form model တည်ဆောက်တာကြောင့် field နည်းသောရိုးရှင်းတဲ့ forms အတွက်သင့်တော်ပါတယ်။ Name attributes, ngModel state, validation flags နဲ့ submit lifecycle ကိုနားလည်ပြီး domain model နဲ့ raw form input ကိုခွဲစဉ်းစားပါ။ Async/complex cross-field rules သို့ dynamic forms များလာလျှင် reactive forms ကပိုရှင်းနိုင်ပါတယ်။
- Valid state / feedback
}Contact form တစ်ခုမှာ required/email/minlength rules, touched-based messages, submit disable နဲ့ reset behavior ထည့်ပြီး keyboard submit စမ်းပါ။
- User input
- Binding + validation
- Valid state / feedback
Success
Template-Driven Forms ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Error message အားလုံးကို page စပေါ်ကတည်းကပြခြင်းက user experience ဆိုးပြီး form state ကိုမသုံးနိုင်တာပြပါတယ်။
Template-Driven Forms များသည် HTML template ထဲတွင် ngModel directive ဖြင့် forms များ တည်ဆောက်သည်။
⚠️ Important: FormsModule ကို import လုပ်ရမည်။
// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule]
})
// component.html
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
<div>
<label>Name:</label>
<input type="text" name="name" [(ngModel)]="user.name" required>
</div>
<div>
<label>Email:</label>
<input type="email" name="email" [(ngModel)]="user.email" required email>
</div>
<button type="submit" [disabled]="!userForm.valid">Submit</button>
</form>
// component.ts
export class MyComponent {
user = { name: '', email: '' };
onSubmit(form: NgForm) {
console.log('Form Data:', this.user);
console.log('Form Valid:', form.valid);
}
}(Form သည် validation ပါဝင်ပြီး valid ဖြစ်မှသာ submit လုပ်နိုင်သည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Contact form တစ်ခုမှာ required/email/minlength rules, touched-based messages, submit disable နဲ့ reset behavior ထည့်ပြီး keyboard submit စမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Template-Driven Forms နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule]
})
// component.html
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
<div>
<label>Name:</label>
<input type="text" name="name" [(ngModel)]="user.name" required>
</div>
<div>
<label>Email:</label>
<input type="email" name="email" [(ngModel)]="user.email" required email>
</div>
<button type="submit" [disabled]="!userForm.valid">Submit</button>
</form>
// component.ts
export class MyComponent {
user = { name: '', email: '' };
onSubmit(form: NgForm) {
console.log('Form Data:', this.user);
console.log('Form Valid:', form.valid);
}
}ရလဒ်: Template-Driven Forms ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Contact form တစ်ခုမှာ required/email/minlength rules, touched-based messages, submit disable နဲ့ reset behavior ထည့်ပြီး keyboard submit စမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- User input → Binding + validation → Valid state / feedback
- Template-driven forms က directives နဲ့ template declarations ကိုသုံးပြီး form model တည်ဆောက်တာကြောင့် field နည်းသောရိုးရှင်းတဲ့ forms အတွက်သင့်တော်ပါတယ်။ Name attributes, ngModel state, validation flags နဲ့ submit lifecycle ကိုနားလည်ပြီး domain model နဲ့ raw form input ကိုခွဲစဉ်းစားပါ။ Async/complex cross-field rules သို့ dynamic forms များလာလျှင် reactive forms ကပိုရှင်းနိုင်ပါတယ်။
- Error message အားလုံးကို page စပေါ်ကတည်းကပြခြင်းက user experience ဆိုးပြီး form state ကိုမသုံးနိုင်တာပြပါတယ်။
- Contact form တစ်ခုမှာ required/email/minlength rules, touched-based messages, submit disable နဲ့ reset behavior ထည့်ပြီး keyboard submit စမ်းပါ။
Angular — Forms — Angular