ရိုးရိုးလေး ရှင်းပြမယ်
Structural: *ngFor ကို ရိုးရိုးလေးနားလည်မယ်
*ngFor directive ကို array တစ်ခုကို loop ပတ်ပြီး list of elements များကို render လုပ်ရန် အသုံးပြုသည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Index ကို identity အဖြစ်သုံးခြင်းက reorder/insert နောက် wrong row DOM state ပြန်သုံးနိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
List rendering မှာ iterable item တစ်ခုစီအတွက် embedded view ဖန်တီးပြီး identity tracking ဖြင့် DOM ကိုreuse လုပ်ပါတယ်။ Legacy `*ngFor` နှင့် modern `@for` နှစ်မျိုးရှိပြီး stable unique ID နဲ့ track လုပ်တာက reorder/update performance နဲ့ local DOM state ကိုမှန်စေပါတယ်။ Filter/sort ကို template ထဲမလုပ်ဘဲ state derivation တစ်နေရာတည်းမှာထားပါ။
- Input + state
- Component + template
- Rendered Angular UI
Structural: *ngFor ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
<ul>
<li *ngFor="let user of users">
{{ user.name }} ({{ user.age }})
</li>
</ul>
// component.ts
export class MyComponent {
users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 }
];
}List rendering မှာ iterable item တစ်ခုစီအတွက် embedded view ဖန်တီးပြီး identity tracking ဖြင့် DOM ကိုreuse လုပ်ပါတယ်။ Legacy `*ngFor` နှင့် modern `@for` နှစ်မျိုးရှိပြီး stable unique ID နဲ့ track လုပ်တာက reorder/update performance နဲ့ local DOM state ကိုမှန်စေပါတယ်။ Filter/sort ကို template ထဲမလုပ်ဘဲ state derivation တစ်နေရာတည်းမှာထားပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
<ul>*ngFor directive ကို array တစ်ခုကို loop ပတ်ပြီး list of elements များကို render လုပ်ရန် အသုံးပြုသည်။
- Component + template
export class MyComponent {List rendering မှာ iterable item တစ်ခုစီအတွက် embedded view ဖန်တီးပြီး identity tracking ဖြင့် DOM ကိုreuse လုပ်ပါတယ်။ Legacy `*ngFor` နှင့် modern `@for` နှစ်မျိုးရှိပြီး stable unique ID နဲ့ track လုပ်တာက reorder/update performance နဲ့ local DOM state ကိုမှန်စေပါတယ်။ Filter/sort ကို template ထဲမလုပ်ဘဲ state derivation တစ်နေရာတည်းမှာထားပါ။
- Rendered Angular UI
}Editable task list ကို stable id ဖြင့် track လုပ်ပြီး item add, delete, reorder လုပ်ချိန် focus နဲ့ input value မမှားကြောင်းစစ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Structural: *ngFor ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Index ကို identity အဖြစ်သုံးခြင်းက reorder/insert နောက် wrong row DOM state ပြန်သုံးနိုင်ပါတယ်။
*ngFor directive ကို array တစ်ခုကို loop ပတ်ပြီး list of elements များကို render လုပ်ရန် အသုံးပြုသည်။
<ul>
<li *ngFor="let user of users">
{{ user.name }} ({{ user.age }})
</li>
</ul>
// component.ts
export class MyComponent {
users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 }
];
}(User ၃ ယောက်၏ နာမည်နှင့်အသက်ပါသော list တစ်ခု ပေါ်လာမည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Editable task list ကို stable id ဖြင့် track လုပ်ပြီး item add, delete, reorder လုပ်ချိန် focus နဲ့ input value မမှားကြောင်းစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Structural: *ngFor နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
<ul>
<li *ngFor="let user of users">
{{ user.name }} ({{ user.age }})
</li>
</ul>
// component.ts
export class MyComponent {
users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 }
];
}ရလဒ်: Structural: *ngFor ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Editable task list ကို stable id ဖြင့် track လုပ်ပြီး item add, delete, reorder လုပ်ချိန် focus နဲ့ input value မမှားကြောင်းစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- List rendering မှာ iterable item တစ်ခုစီအတွက် embedded view ဖန်တီးပြီး identity tracking ဖြင့် DOM ကိုreuse လုပ်ပါတယ်။ Legacy `*ngFor` နှင့် modern `@for` နှစ်မျိုးရှိပြီး stable unique ID နဲ့ track လုပ်တာက reorder/update performance နဲ့ local DOM state ကိုမှန်စေပါတယ်။ Filter/sort ကို template ထဲမလုပ်ဘဲ state derivation တစ်နေရာတည်းမှာထားပါ။
- Index ကို identity အဖြစ်သုံးခြင်းက reorder/insert နောက် wrong row DOM state ပြန်သုံးနိုင်ပါတယ်။
- Editable task list ကို stable id ဖြင့် track လုပ်ပြီး item add, delete, reorder လုပ်ချိန် focus နဲ့ input value မမှားကြောင်းစစ်ပါ။
Angular — Templates — Angular