ရိုးရိုးလေး ရှင်းပြမယ်
Content Projection (ng-content) ကို ရိုးရိုးလေးနားလည်မယ်
Content Projection (ng-content) သည် parent component မှ content ကို child component ထဲသို့ pass လုပ်ရန် အသုံးပြုသည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Projection slot selector မကိုက်လျှင် content တိတ်တဆိတ်မပေါ်နိုင်တာကြောင့် public markup contract ကိုရှင်းရပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Content projection က parent-authored markup ကိုchild component layout slots ထဲထည့်ပြီး reusable shell/API ဖန်တီးပါတယ်။ Single/multiple slots, select matching နဲ့ fallback content ကိုသုံးနိုင်သော်လည်း projected content ၏ data context နဲ့ styling ownership ကိုနားလည်ရပါတယ်။ Card/modal လို layout composition အတွက်ကောင်းပြီး dynamic template instantiation လိုရင် template outlets ကိုစဉ်းစားပါ။
- Input + state
- Component + template
- Rendered Angular UI
Content Projection (ng-content) ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// card.component.ts
@Component({
selector: 'app-card',
template: `
<div class="card">
<div class="card-header">
<ng-content select="[header]"></ng-content>
</div>
<div class="card-body">
<ng-content></ng-content>
</div>
<div class="card-footer">
<ng-content select="[footer]"></ng-content>
</div>
</div>
`
})
export class CardComponent { }
// Usage in parent
<app-card>
<h2 header>Card Title</h2>
<p>This is the main content of the card.</p>
<button footer>Action</button>
</app-card>Content projection က parent-authored markup ကိုchild component layout slots ထဲထည့်ပြီး reusable shell/API ဖန်တီးပါတယ်။ Single/multiple slots, select matching နဲ့ fallback content ကိုသုံးနိုင်သော်လည်း projected content ၏ data context နဲ့ styling ownership ကိုနားလည်ရပါတယ်။ Card/modal လို layout composition အတွက်ကောင်းပြီး dynamic template instantiation လိုရင် template outlets ကိုစဉ်းစားပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
// card.component.tsContent Projection (ng-content) သည် parent component မှ content ကို child component ထဲသို့ pass လုပ်ရန် အသုံးပြုသည်။
- Component + template
<ng-content select="[footer]"></ng-content>Content projection က parent-authored markup ကိုchild component layout slots ထဲထည့်ပြီး reusable shell/API ဖန်တီးပါတယ်။ Single/multiple slots, select matching နဲ့ fallback content ကိုသုံးနိုင်သော်လည်း projected content ၏ data context နဲ့ styling ownership ကိုနားလည်ရပါတယ်။ Card/modal လို layout composition အတွက်ကောင်းပြီး dynamic template instantiation လိုရင် template outlets ကိုစဉ်းစားပါ။
- Rendered Angular UI
</app-card>Card component ကို header/body/actions slots သုံးခုနဲ့ရေးပြီး optional actions, fallback title နဲ့ projected interactive content ကိုစမ်းပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Content Projection (ng-content) ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Projection slot selector မကိုက်လျှင် content တိတ်တဆိတ်မပေါ်နိုင်တာကြောင့် public markup contract ကိုရှင်းရပါတယ်။
Content Projection (ng-content) သည် parent component မှ content ကို child component ထဲသို့ pass လုပ်ရန် အသုံးပြုသည်။
// card.component.ts
@Component({
selector: 'app-card',
template: `
<div class="card">
<div class="card-header">
<ng-content select="[header]"></ng-content>
</div>
<div class="card-body">
<ng-content></ng-content>
</div>
<div class="card-footer">
<ng-content select="[footer]"></ng-content>
</div>
</div>
`
})
export class CardComponent { }
// Usage in parent
<app-card>
<h2 header>Card Title</h2>
<p>This is the main content of the card.</p>
<button footer>Action</button>
</app-card>(Parent မှ pass လုပ်လိုက်သော content များသည် card component ၏ designated slots များတွင် ပေါ်လာမည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Card component ကို header/body/actions slots သုံးခုနဲ့ရေးပြီး optional actions, fallback title နဲ့ projected interactive content ကိုစမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Content Projection (ng-content) နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// card.component.ts
@Component({
selector: 'app-card',
template: `
<div class="card">
<div class="card-header">
<ng-content select="[header]"></ng-content>
</div>
<div class="card-body">
<ng-content></ng-content>
</div>
<div class="card-footer">
<ng-content select="[footer]"></ng-content>
</div>
</div>
`
})
export class CardComponent { }
// Usage in parent
<app-card>
<h2 header>Card Title</h2>
<p>This is the main content of the card.</p>
<button footer>Action</button>
</app-card>ရလဒ်: Content Projection (ng-content) ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Card component ကို header/body/actions slots သုံးခုနဲ့ရေးပြီး optional actions, fallback title နဲ့ projected interactive content ကိုစမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Content projection က parent-authored markup ကိုchild component layout slots ထဲထည့်ပြီး reusable shell/API ဖန်တီးပါတယ်။ Single/multiple slots, select matching နဲ့ fallback content ကိုသုံးနိုင်သော်လည်း projected content ၏ data context နဲ့ styling ownership ကိုနားလည်ရပါတယ်။ Card/modal လို layout composition အတွက်ကောင်းပြီး dynamic template instantiation လိုရင် template outlets ကိုစဉ်းစားပါ။
- Projection slot selector မကိုက်လျှင် content တိတ်တဆိတ်မပေါ်နိုင်တာကြောင့် public markup contract ကိုရှင်းရပါတယ်။
- Card component ကို header/body/actions slots သုံးခုနဲ့ရေးပြီး optional actions, fallback title နဲ့ projected interactive content ကိုစမ်းပါ။
Angular — Components — Angular