Thuta Learning
Angular
AdvancedWeb Developmentအလယ်အလတ်

Dynamic Components

Angularသင်ခန်းစာ 4225 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Dynamic Components ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Dynamic Components ကို ရိုးရိုးလေးနားလည်မယ်

Dynamic Components များကို runtime တွင် programmatically create လုပ်နိုင်သည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Dynamic component reference ကိုdestroy မလုပ်ခြင်းက views/subscriptions ကျန်ပြီး memory leak ဖြစ်စေနိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Dynamic components က runtime config အရ component type ကိုဖန်တီးရန်လိုသော dialogs, dashboards နဲ့ plugin-like UI တွေအတွက်အသုံးဝင်ပါတယ်။ Declarative control flow ဖြင့်ဖြေရှင်းနိုင်ရင်အဲဒါကိုဦးစားပေးပြီး imperative creation တွင် inputs, outputs, injector, host location နဲ့ destroy lifecycle ကိုတစ်စုတည်းကိုင်ပါ။ Untrusted component type/config ကို allowlist မရှိဘဲ load မလုပ်ပါနှင့်။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Dynamic Components ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// dynamic-host.directive.ts
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appDynamicHost]'
})
export class DynamicHostDirective {
  constructor(public viewContainerRef: ViewContainerRef) {}
}

// parent.component.ts
import { ComponentFactoryResolver, ViewChild } from '@angular/core';

export class ParentComponent {
  @ViewChild(DynamicHostDirective, { static: true }) 
  dynamicHost!: DynamicHostDirective;
  
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
  
  loadComponent() {
    const viewContainerRef = this.dynamicHost.viewContainerRef;
    viewContainerRef.clear();
    
    const componentRef = viewContainerRef.createComponent(AlertComponent);
    componentRef.instance.message = 'Dynamic Alert!';
  }
}

// parent.component.html
<div appDynamicHost></div>
<button (click)="loadComponent()">Load Component</button>

Dynamic components က runtime config အရ component type ကိုဖန်တီးရန်လိုသော dialogs, dashboards နဲ့ plugin-like UI တွေအတွက်အသုံးဝင်ပါတယ်။ Declarative control flow ဖြင့်ဖြေရှင်းနိုင်ရင်အဲဒါကိုဦးစားပေးပြီး imperative creation တွင် inputs, outputs, injector, host location နဲ့ destroy lifecycle ကိုတစ်စုတည်းကိုင်ပါ။ Untrusted component type/config ကို allowlist မရှိဘဲ load မလုပ်ပါနှင့်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input + state// dynamic-host.directive.ts

    Dynamic Components များကို runtime တွင် programmatically create လုပ်နိုင်သည်။

  2. Component + templatedynamicHost!: DynamicHostDirective;

    Dynamic components က runtime config အရ component type ကိုဖန်တီးရန်လိုသော dialogs, dashboards နဲ့ plugin-like UI တွေအတွက်အသုံးဝင်ပါတယ်။ Declarative control flow ဖြင့်ဖြေရှင်းနိုင်ရင်အဲဒါကိုဦးစားပေးပြီး imperative creation တွင် inputs, outputs, injector, host location နဲ့ destroy lifecycle ကိုတစ်စုတည်းကိုင်ပါ။ Untrusted component type/config ကို allowlist မရှိဘဲ load မလုပ်ပါနှင့်။

  3. Rendered Angular UI<button (click)="loadComponent()">Load Component</but...

    Widget registry တစ်ခုက allowlisted component နှစ်မျိုးကို config အရrender လုပ်အောင်ရေးပြီး input update, output event နဲ့ removal cleanup ကိုစမ်းပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Success

Dynamic Components ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Dynamic component reference ကိုdestroy မလုပ်ခြင်းက views/subscriptions ကျန်ပြီး memory leak ဖြစ်စေနိုင်ပါတယ်။

Dynamic Components များကို runtime တွင် programmatically create လုပ်နိုင်သည်။

typescript
// dynamic-host.directive.ts
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appDynamicHost]'
})
export class DynamicHostDirective {
  constructor(public viewContainerRef: ViewContainerRef) {}
}

// parent.component.ts
import { ComponentFactoryResolver, ViewChild } from '@angular/core';

export class ParentComponent {
  @ViewChild(DynamicHostDirective, { static: true }) 
  dynamicHost!: DynamicHostDirective;
  
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
  
  loadComponent() {
    const viewContainerRef = this.dynamicHost.viewContainerRef;
    viewContainerRef.clear();
    
    const componentRef = viewContainerRef.createComponent(AlertComponent);
    componentRef.instance.message = 'Dynamic Alert!';
  }
}

// parent.component.html
<div appDynamicHost></div>
<button (click)="loadComponent()">Load Component</button>
You should see
(Button ကိုနှိပ်လျှင် AlertComponent သည် runtime တွင် dynamically create ဖြစ်မည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Widget registry တစ်ခုက allowlisted component နှစ်မျိုးကို config အရrender လုပ်အောင်ရေးပြီး input update, output event နဲ့ removal cleanup ကိုစမ်းပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Dynamic Components နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// dynamic-host.directive.ts
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appDynamicHost]'
})
export class DynamicHostDirective {
  constructor(public viewContainerRef: ViewContainerRef) {}
}

// parent.component.ts
import { ComponentFactoryResolver, ViewChild } from '@angular/core';

export class ParentComponent {
  @ViewChild(DynamicHostDirective, { static: true }) 
  dynamicHost!: DynamicHostDirective;
  
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
  
  loadComponent() {
    const viewContainerRef = this.dynamicHost.viewContainerRef;
    viewContainerRef.clear();
    
    const componentRef = viewContainerRef.createComponent(AlertComponent);
    componentRef.instance.message = 'Dynamic Alert!';
  }
}

// parent.component.html
<div appDynamicHost></div>
<button (click)="loadComponent()">Load Component</button>

ရလဒ်: Dynamic Components ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Widget registry တစ်ခုက allowlisted component နှစ်မျိုးကို config အရrender လုပ်အောင်ရေးပြီး input update, output event နဲ့ removal cleanup ကိုစမ်းပါ။

Quick Quiz 1 / 3

Dynamic Components ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Input + state → Component + template → Rendered Angular UI
  • Dynamic components က runtime config အရ component type ကိုဖန်တီးရန်လိုသော dialogs, dashboards နဲ့ plugin-like UI တွေအတွက်အသုံးဝင်ပါတယ်။ Declarative control flow ဖြင့်ဖြေရှင်းနိုင်ရင်အဲဒါကိုဦးစားပေးပြီး imperative creation တွင် inputs, outputs, injector, host location နဲ့ destroy lifecycle ကိုတစ်စုတည်းကိုင်ပါ။ Untrusted component type/config ကို allowlist မရှိဘဲ load မလုပ်ပါနှင့်။
  • Dynamic component reference ကိုdestroy မလုပ်ခြင်းက views/subscriptions ကျန်ပြီး memory leak ဖြစ်စေနိုင်ပါတယ်။
  • Widget registry တစ်ခုက allowlisted component နှစ်မျိုးကို config အရrender လုပ်အောင်ရေးပြီး input update, output event နဲ့ removal cleanup ကိုစမ်းပါ။

Angular — Components — Angular

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Dynamic component reference ကိုdestroy မလုပ်ခြင်းက views/subscriptions ကျန်ပြီး memory leak ဖြစ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

Dynamic Components နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Button ကိုနှိပ်လျှင် AlertComponent သည် runtime တွင် dynamically create ဖြစ်မည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Dynamic Components | Thuta Learning