ရိုးရိုးလေး ရှင်းပြမယ်
Structural: *ngSwitch ကို ရိုးရိုးလေးနားလည်မယ်
*ngSwitch directive သည် multiple conditions များအတွက် အသုံးပြုသည် (JavaScript ၏ switch statement ကဲ့သို့)။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Unrelated booleans များကို switch တစ်ခုထဲအတင်းထည့်ခြင်းက impossible combinations ကိုဖုံးပြီး state model ရှုပ်စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Switch rendering က mutually exclusive state branches များကို one discriminant ဖြင့်ဖော်ပြရန်သင့်တော်ပါတယ်။ Enum သို့ union state သုံးလျှင် unknown state ကို compiler/design အဆင့်မှာရှာလွယ်ပြီး default branch က defensive fallback ပေးပါတယ်။ Modern `@switch` နဲ့ legacy `ngSwitch` တို့ကိုဖတ်နိုင်ပြီး duplicated branch markup ကို child component အဖြစ်ခွဲပါ။
- Input + state
- Component + template
- Rendered Angular UI
Structural: *ngSwitch ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
<div [ngSwitch]="userRole">
<p *ngSwitchCase="'admin'">👑 Admin Dashboard</p>
<p *ngSwitchCase="'editor'">✏️ Editor Panel</p>
<p *ngSwitchCase="'viewer'">👀 Viewer Mode</p>
<p *ngSwitchDefault>🔒 Access Denied</p>
</div>
// component.ts
export class MyComponent {
userRole = 'editor';
}Switch rendering က mutually exclusive state branches များကို one discriminant ဖြင့်ဖော်ပြရန်သင့်တော်ပါတယ်။ Enum သို့ union state သုံးလျှင် unknown state ကို compiler/design အဆင့်မှာရှာလွယ်ပြီး default branch က defensive fallback ပေးပါတယ်။ Modern `@switch` နဲ့ legacy `ngSwitch` တို့ကိုဖတ်နိုင်ပြီး duplicated branch markup ကို child component အဖြစ်ခွဲပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
<div [ngSwitch]="userRole">*ngSwitch directive သည် multiple conditions များအတွက် အသုံးပြုသည် (JavaScript ၏ switch statement ကဲ့သို့)။
- Component + template
</div>Switch rendering က mutually exclusive state branches များကို one discriminant ဖြင့်ဖော်ပြရန်သင့်တော်ပါတယ်။ Enum သို့ union state သုံးလျှင် unknown state ကို compiler/design အဆင့်မှာရှာလွယ်ပြီး default branch က defensive fallback ပေးပါတယ်။ Modern `@switch` နဲ့ legacy `ngSwitch` တို့ကိုဖတ်နိုင်ပြီး duplicated branch markup ကို child component အဖြစ်ခွဲပါ။
- Rendered Angular UI
}Order status union ကို pending/paid/shipped/cancelled branch လေးခုနဲ့ပြပြီး unknown API value အတွက် fallback ထည့်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Structural: *ngSwitch ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Unrelated booleans များကို switch တစ်ခုထဲအတင်းထည့်ခြင်းက impossible combinations ကိုဖုံးပြီး state model ရှုပ်စေပါတယ်။
*ngSwitch directive သည် multiple conditions များအတွက် အသုံးပြုသည် (JavaScript ၏ switch statement ကဲ့သို့)။
<div [ngSwitch]="userRole">
<p *ngSwitchCase="'admin'">👑 Admin Dashboard</p>
<p *ngSwitchCase="'editor'">✏️ Editor Panel</p>
<p *ngSwitchCase="'viewer'">👀 Viewer Mode</p>
<p *ngSwitchDefault>🔒 Access Denied</p>
</div>
// component.ts
export class MyComponent {
userRole = 'editor';
}(userRole သည် 'editor' ဖြစ်သောကြောင့် '✏️ Editor Panel' ဟု ပြသမည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Order status union ကို pending/paid/shipped/cancelled branch လေးခုနဲ့ပြပြီး unknown API value အတွက် fallback ထည့်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Structural: *ngSwitch နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
<div [ngSwitch]="userRole">
<p *ngSwitchCase="'admin'">👑 Admin Dashboard</p>
<p *ngSwitchCase="'editor'">✏️ Editor Panel</p>
<p *ngSwitchCase="'viewer'">👀 Viewer Mode</p>
<p *ngSwitchDefault>🔒 Access Denied</p>
</div>
// component.ts
export class MyComponent {
userRole = 'editor';
}ရလဒ်: Structural: *ngSwitch ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Order status union ကို pending/paid/shipped/cancelled branch လေးခုနဲ့ပြပြီး unknown API value အတွက် fallback ထည့်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Switch rendering က mutually exclusive state branches များကို one discriminant ဖြင့်ဖော်ပြရန်သင့်တော်ပါတယ်။ Enum သို့ union state သုံးလျှင် unknown state ကို compiler/design အဆင့်မှာရှာလွယ်ပြီး default branch က defensive fallback ပေးပါတယ်။ Modern `@switch` နဲ့ legacy `ngSwitch` တို့ကိုဖတ်နိုင်ပြီး duplicated branch markup ကို child component အဖြစ်ခွဲပါ။
- Unrelated booleans များကို switch တစ်ခုထဲအတင်းထည့်ခြင်းက impossible combinations ကိုဖုံးပြီး state model ရှုပ်စေပါတယ်။
- Order status union ကို pending/paid/shipped/cancelled branch လေးခုနဲ့ပြပြီး unknown API value အတွက် fallback ထည့်ပါ။
Angular — Templates — Angular