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

NgModules

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

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

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

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

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

NgModule သည် Angular application ကို organize လုပ်ရန် အသုံးပြုသည်။ Components, Directives, Pipes, နှင့် Services များကို logical groups အဖြစ် ခွဲခြားသည်။

Problem → Why → Solution

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

Module တိုင်းမှာ service provider ထပ်ကြေညာခြင်းက မရည်ရွယ်ဘဲ service instance အများအပြားဖြစ်စေနိုင်ပါတယ်။

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

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

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

NgModule က declarations, imports, providers နဲ့ bootstrap configuration ကိုစုစည်းသည့် Angular architecture မူလပုံစံဖြစ်ပြီး legacy/large codebases များမှာအများကြီးတွေ့ရပါတယ်။ Standalone APIs က dependency များကို component/route အနီးမှာထားပြီး module boilerplate လျှော့ပေးပါတယ်။ နှစ်မျိုးလုံးမှာ provider scope, lazy boundary နဲ့ import direction ကိုနားလည်မှ duplicate instances နဲ့ circular dependencies ရှောင်နိုင်ပါတယ်။

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

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

အဓိက Syntax / Pattern

typescript
// app.module.ts - Main/Root Module
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [  // Components, Directives, Pipes
    AppComponent,
    HeaderComponent,
    FooterComponent
  ],
  imports: [       // Other modules
    BrowserModule,
    FormsModule,
    AppRoutingModule
  ],
  providers: [],   // Services
  bootstrap: [AppComponent]  // Root component
})
export class AppModule { }

// feature.module.ts - Feature Module
@NgModule({
  declarations: [
    UserListComponent,
    UserDetailComponent
  ],
  imports: [
    CommonModule,
    SharedModule
  ],
  exports: [UserListComponent]  // Available to other modules
})
export class UserModule { }

NgModule က declarations, imports, providers နဲ့ bootstrap configuration ကိုစုစည်းသည့် Angular architecture မူလပုံစံဖြစ်ပြီး legacy/large codebases များမှာအများကြီးတွေ့ရပါတယ်။ Standalone APIs က dependency များကို component/route အနီးမှာထားပြီး module boilerplate လျှော့ပေးပါတယ်။ နှစ်မျိုးလုံးမှာ provider scope, lazy boundary နဲ့ import direction ကိုနားလည်မှ duplicate instances နဲ့ circular dependencies ရှောင်နိုင်ပါတယ်။

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

  1. Input + state// app.module.ts - Main/Root Module

    NgModule သည် Angular application ကို organize လုပ်ရန် အသုံးပြုသည်။ Components, Directives, Pipes, နှင့် Services များကို logical groups အဖြစ် ခွဲခြားသည်။

  2. Component + templatebootstrap: [AppComponent] // Root component

    NgModule က declarations, imports, providers နဲ့ bootstrap configuration ကိုစုစည်းသည့် Angular architecture မူလပုံစံဖြစ်ပြီး legacy/large codebases များမှာအများကြီးတွေ့ရပါတယ်။ Standalone APIs က dependency များကို component/route အနီးမှာထားပြီး module boilerplate လျှော့ပေးပါတယ်။ နှစ်မျိုးလုံးမှာ provider scope, lazy boundary နဲ့ import direction ကိုနားလည်မှ duplicate instances နဲ့ circular dependencies ရှောင်နိုင်ပါတယ်။

  3. Rendered Angular UIexport class UserModule { }

    NgModule feature တစ်ခုနဲ့ standalone feature တစ်ခုကို route နှစ်ခုအဖြစ်တည်ဆောက်ပြီး imports/providers ဘယ်မှာကြေညာသလဲနှိုင်းပါ။

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

Success

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

Failure

Module တိုင်းမှာ service provider ထပ်ကြေညာခြင်းက မရည်ရွယ်ဘဲ service instance အများအပြားဖြစ်စေနိုင်ပါတယ်။

NgModule သည် Angular application ကို organize လုပ်ရန် အသုံးပြုသည်။ Components, Directives, Pipes, နှင့် Services များကို logical groups အဖြစ် ခွဲခြားသည်။

typescript
// app.module.ts - Main/Root Module
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [  // Components, Directives, Pipes
    AppComponent,
    HeaderComponent,
    FooterComponent
  ],
  imports: [       // Other modules
    BrowserModule,
    FormsModule,
    AppRoutingModule
  ],
  providers: [],   // Services
  bootstrap: [AppComponent]  // Root component
})
export class AppModule { }

// feature.module.ts - Feature Module
@NgModule({
  declarations: [
    UserListComponent,
    UserDetailComponent
  ],
  imports: [
    CommonModule,
    SharedModule
  ],
  exports: [UserListComponent]  // Available to other modules
})
export class UserModule { }
You should see
(Application သည် modules များဖြင့် organized ဖြစ်ပြီး reusable ဖြစ်လာသည်)

Real-world usage

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

NgModule feature တစ်ခုနဲ့ standalone feature တစ်ခုကို route နှစ်ခုအဖြစ်တည်ဆောက်ပြီး imports/providers ဘယ်မှာကြေညာသလဲနှိုင်းပါ။

Mini challenge

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

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

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// app.module.ts - Main/Root Module
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [  // Components, Directives, Pipes
    AppComponent,
    HeaderComponent,
    FooterComponent
  ],
  imports: [       // Other modules
    BrowserModule,
    FormsModule,
    AppRoutingModule
  ],
  providers: [],   // Services
  bootstrap: [AppComponent]  // Root component
})
export class AppModule { }

// feature.module.ts - Feature Module
@NgModule({
  declarations: [
    UserListComponent,
    UserDetailComponent
  ],
  imports: [
    CommonModule,
    SharedModule
  ],
  exports: [UserListComponent]  // Available to other modules
})
export class UserModule { }

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

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

NgModule feature တစ်ခုနဲ့ standalone feature တစ်ခုကို route နှစ်ခုအဖြစ်တည်ဆောက်ပြီး imports/providers ဘယ်မှာကြေညာသလဲနှိုင်းပါ။

Quick Quiz 1 / 3

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

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Input + state → Component + template → Rendered Angular UI
  • NgModule က declarations, imports, providers နဲ့ bootstrap configuration ကိုစုစည်းသည့် Angular architecture မူလပုံစံဖြစ်ပြီး legacy/large codebases များမှာအများကြီးတွေ့ရပါတယ်။ Standalone APIs က dependency များကို component/route အနီးမှာထားပြီး module boilerplate လျှော့ပေးပါတယ်။ နှစ်မျိုးလုံးမှာ provider scope, lazy boundary နဲ့ import direction ကိုနားလည်မှ duplicate instances နဲ့ circular dependencies ရှောင်နိုင်ပါတယ်။
  • Module တိုင်းမှာ service provider ထပ်ကြေညာခြင်းက မရည်ရွယ်ဘဲ service instance အများအပြားဖြစ်စေနိုင်ပါတယ်။
  • NgModule feature တစ်ခုနဲ့ standalone feature တစ်ခုကို route နှစ်ခုအဖြစ်တည်ဆောက်ပြီး imports/providers ဘယ်မှာကြေညာသလဲနှိုင်းပါ။

Angular — Dependency Injection — Angular

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

  • Module တိုင်းမှာ service provider ထပ်ကြေညာခြင်းက မရည်ရွယ်ဘဲ service instance အများအပြားဖြစ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Application သည် modules များဖြင့် organized ဖြစ်ပြီး reusable ဖြစ်လာသည်)

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

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

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

NgModules | Thuta Learning