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

Async Pipe

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

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

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

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

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

Async Pipe သည် Observables နှင့် Promises များကို template ထဲတွင် တိုက်ရိုက် subscribe လုပ်ရန် အသုံးပြုသည်။ ၎င်းသည် automatic unsubscribe လုပ်ပေးသည်။

Problem → Why → Solution

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

Cold HTTP observable ကို async pipe အများကြီးနဲ့ bind လုပ်ခြင်းက network request ထပ်ခေါ်စေနိုင်ပါတယ်။

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

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

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

Async pipe က Observable/Promise ကိုsubscribe လုပ်၊ latest value ပြပြီး view destroy သို့ source reference ပြောင်းချိန် unsubscribe လုပ်ပေးပါတယ်။ Template ထဲ same cold observable ကို async pipe အကြိမ်များစွာသုံးလျှင် request/work ထပ်ဖြစ်နိုင်တာကြောင့် result ကိုတစ်ကြိမ် alias လုပ်သို့ shared stream သုံးပါ။ Loading/error/empty states က stream value တစ်ခုတည်းထက်သီးခြား model လိုနိုင်ပါတယ်။

Mental Model
  1. Event / request
  2. Stream + service
  3. State / UI update

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

အဓိက Syntax / Pattern

typescript
// component.ts
import { Observable } from 'rxjs';

export class MyComponent {
  time$: Observable<Date>;
  users$: Observable<any[]>;
  
  constructor(private dataService: DataService) {
    // Observable that emits every second
    this.time$ = new Observable(observer => {
      setInterval(() => observer.next(new Date()), 1000);
    });
    
    // Observable from HTTP request
    this.users$ = this.dataService.getUsers();
  }
}

// component.html
<p>Current Time: {{ time$ | async | date:'medium' }}</p>

<div *ngIf="users$ | async as users; else loading">
  <ul>
    <li *ngFor="let user of users">{{ user.name }}</li>
  </ul>
</div>

<ng-template #loading>
  <p>Loading users...</p>
</ng-template>

Async pipe က Observable/Promise ကိုsubscribe လုပ်၊ latest value ပြပြီး view destroy သို့ source reference ပြောင်းချိန် unsubscribe လုပ်ပေးပါတယ်။ Template ထဲ same cold observable ကို async pipe အကြိမ်များစွာသုံးလျှင် request/work ထပ်ဖြစ်နိုင်တာကြောင့် result ကိုတစ်ကြိမ် alias လုပ်သို့ shared stream သုံးပါ။ Loading/error/empty states က stream value တစ်ခုတည်းထက်သီးခြား model လိုနိုင်ပါတယ်။

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

  1. Event / request// component.ts

    Async Pipe သည် Observables နှင့် Promises များကို template ထဲတွင် တိုက်ရိုက် subscribe လုပ်ရန် အသုံးပြုသည်။ ၎င်းသည် automatic unsubscribe လုပ်ပေးသည်။

  2. Stream + service}

    Async pipe က Observable/Promise ကိုsubscribe လုပ်၊ latest value ပြပြီး view destroy သို့ source reference ပြောင်းချိန် unsubscribe လုပ်ပေးပါတယ်။ Template ထဲ same cold observable ကို async pipe အကြိမ်များစွာသုံးလျှင် request/work ထပ်ဖြစ်နိုင်တာကြောင့် result ကိုတစ်ကြိမ် alias လုပ်သို့ shared stream သုံးပါ။ Loading/error/empty states က stream value တစ်ခုတည်းထက်သီးခြား model လိုနိုင်ပါတယ်။

  3. State / UI update</ng-template>

    User stream တစ်ခုကို template မှာတစ်ကြိမ် alias လုပ်ပြီး loading/error/empty/success states ပြပါ။ Navigate away လုပ်ချိန် cleanup ကိုlog နဲ့စစ်ပါ။

Execution Flow
  1. Event / request
  2. Stream + service
  3. State / UI update

Success

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

Failure

Cold HTTP observable ကို async pipe အများကြီးနဲ့ bind လုပ်ခြင်းက network request ထပ်ခေါ်စေနိုင်ပါတယ်။

Async Pipe သည် Observables နှင့် Promises များကို template ထဲတွင် တိုက်ရိုက် subscribe လုပ်ရန် အသုံးပြုသည်။ ၎င်းသည် automatic unsubscribe လုပ်ပေးသည်။

typescript
// component.ts
import { Observable } from 'rxjs';

export class MyComponent {
  time$: Observable<Date>;
  users$: Observable<any[]>;
  
  constructor(private dataService: DataService) {
    // Observable that emits every second
    this.time$ = new Observable(observer => {
      setInterval(() => observer.next(new Date()), 1000);
    });
    
    // Observable from HTTP request
    this.users$ = this.dataService.getUsers();
  }
}

// component.html
<p>Current Time: {{ time$ | async | date:'medium' }}</p>

<div *ngIf="users$ | async as users; else loading">
  <ul>
    <li *ngFor="let user of users">{{ user.name }}</li>
  </ul>
</div>

<ng-template #loading>
  <p>Loading users...</p>
</ng-template>
You should see
(Observable data များသည် အလိုအလျောက် subscribe/unsubscribe ဖြစ်ပြီး memory leaks မရှိပါ)

Real-world usage

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

User stream တစ်ခုကို template မှာတစ်ကြိမ် alias လုပ်ပြီး loading/error/empty/success states ပြပါ။ Navigate away လုပ်ချိန် cleanup ကိုlog နဲ့စစ်ပါ။

Mini challenge

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

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

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// component.ts
import { Observable } from 'rxjs';

export class MyComponent {
  time$: Observable<Date>;
  users$: Observable<any[]>;
  
  constructor(private dataService: DataService) {
    // Observable that emits every second
    this.time$ = new Observable(observer => {
      setInterval(() => observer.next(new Date()), 1000);
    });
    
    // Observable from HTTP request
    this.users$ = this.dataService.getUsers();
  }
}

// component.html
<p>Current Time: {{ time$ | async | date:'medium' }}</p>

<div *ngIf="users$ | async as users; else loading">
  <ul>
    <li *ngFor="let user of users">{{ user.name }}</li>
  </ul>
</div>

<ng-template #loading>
  <p>Loading users...</p>
</ng-template>

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

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

User stream တစ်ခုကို template မှာတစ်ကြိမ် alias လုပ်ပြီး loading/error/empty/success states ပြပါ။ Navigate away လုပ်ချိန် cleanup ကိုlog နဲ့စစ်ပါ။

Quick Quiz 1 / 3

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

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Event / request → Stream + service → State / UI update
  • Async pipe က Observable/Promise ကိုsubscribe လုပ်၊ latest value ပြပြီး view destroy သို့ source reference ပြောင်းချိန် unsubscribe လုပ်ပေးပါတယ်။ Template ထဲ same cold observable ကို async pipe အကြိမ်များစွာသုံးလျှင် request/work ထပ်ဖြစ်နိုင်တာကြောင့် result ကိုတစ်ကြိမ် alias လုပ်သို့ shared stream သုံးပါ။ Loading/error/empty states က stream value တစ်ခုတည်းထက်သီးခြား model လိုနိုင်ပါတယ်။
  • Cold HTTP observable ကို async pipe အများကြီးနဲ့ bind လုပ်ခြင်းက network request ထပ်ခေါ်စေနိုင်ပါတယ်။
  • User stream တစ်ခုကို template မှာတစ်ကြိမ် alias လုပ်ပြီး loading/error/empty/success states ပြပါ။ Navigate away လုပ်ချိန် cleanup ကိုlog နဲ့စစ်ပါ။

Angular — Templates — Angular

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

  • Cold HTTP observable ကို async pipe အများကြီးနဲ့ bind လုပ်ခြင်းက network request ထပ်ခေါ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Observable data များသည် အလိုအလျောက် subscribe/unsubscribe ဖြစ်ပြီး memory leaks မရှိပါ)

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

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

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

Async Pipe | Thuta Learning