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

Exercise Set 2: Services, Forms & RxJS

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

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

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

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

Exercise Set 2: Services, Forms & RxJS ကို ရိုးရိုးလေးနားလည်မယ်

ဒီ exercise set ကတော့ Exercise Set 1 ထက် တစ်ဆင့်တက်ပြီး Intermediate/Advanced chapter တွေမှာ သင်ခဲ့တဲ့ service, reactive form validation, HTTP client နဲ့ RxJS operator (map, filter) တွေကို ပေါင်းစပ်အသုံးချကြည့်ရမှာဖြစ်ပါတယ်။ Task တစ်ခုစီက production app တွေမှာ တွေ့ရလေ့ရှိတဲ့ pattern တွေဖြစ်တဲ့အတွက် ဒီအလေ့အကျင့်တွေက real-world project တည်ဆောက်ရာမှာ တိုက်ရိုက်အသုံးဝင်ပါလိမ့်မယ်။ Task တွေကို အစဉ်လိုက် အခက်တက်မြင့်တက်အောင် စီထားတာမို့ အရင် task ရဲ့ solution ကို နောက် task မှာ reuse လုပ်နိုင်ပါတယ်။

Problem → Why → Solution

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

Happy path subscription တစ်ခုသာရေးပြီး cancellation/error/teardown မစမ်းလျှင် production race conditions ကိုမတွေ့နိုင်ပါ။

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

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

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

ဒီ exercise set က service/DI boundaries, form contracts နဲ့ RxJS async behavior ကိုပေါင်းစပ်စမ်းပါတယ်။ Dependency diagram, form state machine နဲ့ stream timeline ကိုအရင်ရေးပြီး success/error/cancellation/cleanup behavior သတ်မှတ်ပါ။ HTTP နှင့် time dependencies ကိုtest doubles ဖြင့်control လုပ်ပြီး user-observable results ကိုစစ်ပါ။

Mental Model
  1. လိုအပ်ချက်
  2. စဉ်းစားပြီး implement
  3. စမ်းသပ်ပြီးရှင်းပြ

Exercise Set 2: Services, Forms & RxJS ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// counter.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CounterService {
  private countSubject = new BehaviorSubject<number>(0);
  count$ = this.countSubject.asObservable();

  increment() { this.countSubject.next(this.countSubject.value + 1); }
  decrement() { this.countSubject.next(this.countSubject.value - 1); }
  reset() { this.countSubject.next(0); }
}

// user-list.component.ts (task 3 skeleton)
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, filter } from 'rxjs/operators';

interface User { name: string; }

@Component({ selector: 'app-user-list', template: `<li *ngFor="let n of names$ | async">{{ n }}</li>` })
export class UserListComponent implements OnInit {
  names$ = this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
    map(users => users.map(u => u.name)),
    map(names => names.filter(n => n.length > 5))
  );

  constructor(private http: HttpClient) {}
  ngOnInit() {}
}

ဒီ exercise set က service/DI boundaries, form contracts နဲ့ RxJS async behavior ကိုပေါင်းစပ်စမ်းပါတယ်။ Dependency diagram, form state machine နဲ့ stream timeline ကိုအရင်ရေးပြီး success/error/cancellation/cleanup behavior သတ်မှတ်ပါ။ HTTP နှင့် time dependencies ကိုtest doubles ဖြင့်control လုပ်ပြီး user-observable results ကိုစစ်ပါ။

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

  1. လိုအပ်ချက်// counter.service.ts

    ဒီ exercise set ကတော့ Exercise Set 1 ထက် တစ်ဆင့်တက်ပြီး Intermediate/Advanced chapter တွေမှာ သင်ခဲ့တဲ့ service, reactive form validation, HTTP client နဲ့ RxJS operator (map, filter) တွေကို ပေါင်းစပ်အသုံးချကြည့်ရမှာဖြစ်ပါတယ်။ Task တစ်ခုစီက production app တွေမှာ တွေ့ရလေ့ရှိတဲ့ pattern တွေဖြစ်တဲ့အတွက် ဒီအလေ့အကျင့်တွေက real-world project တည်ဆောက်ရာမှာ တိုက်ရိုက်အသုံးဝင်ပါလိမ့်မယ်။ Task တွေကို အစဉ်လိုက် အခက်တက်မြင့်တက်အောင် စီထားတာမို့ အရင် task ရဲ့ solution ကို နောက် task မှာ reuse လုပ်နိုင်ပါတယ်။

  2. စဉ်းစားပြီး implementimport { Component, OnInit } from '@angular/core';

    ဒီ exercise set က service/DI boundaries, form contracts နဲ့ RxJS async behavior ကိုပေါင်းစပ်စမ်းပါတယ်။ Dependency diagram, form state machine နဲ့ stream timeline ကိုအရင်ရေးပြီး success/error/cancellation/cleanup behavior သတ်မှတ်ပါ။ HTTP နှင့် time dependencies ကိုtest doubles ဖြင့်control လုပ်ပြီး user-observable results ကိုစစ်ပါ။

  3. စမ်းသပ်ပြီးရှင်းပြ}

    Exercise တစ်ခုစီမှာ typed service, validated form, operator pipeline နဲ့ tests ပါစေ။ Rapid input, server error, retry policy နဲ့ navigation cleanup ကိုစမ်းပါ။

Execution Flow
  1. လိုအပ်ချက်
  2. စဉ်းစားပြီး implement
  3. စမ်းသပ်ပြီးရှင်းပြ

Success

Exercise Set 2: Services, Forms & RxJS ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Happy path subscription တစ်ခုသာရေးပြီး cancellation/error/teardown မစမ်းလျှင် production race conditions ကိုမတွေ့နိုင်ပါ။

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ exercise set ကတော့ Exercise Set 1 ထက် တစ်ဆင့်တက်ပြီး Intermediate/Advanced chapter တွေမှာ သင်ခဲ့တဲ့ service, reactive form validation, HTTP client နဲ့ RxJS operator (map, filter) တွေကို ပေါင်းစပ်အသုံးချကြည့်ရမှာဖြစ်ပါတယ်။ Task တစ်ခုစီက production app တွေမှာ တွေ့ရလေ့ရှိတဲ့ pattern တွေဖြစ်တဲ့အတွက် ဒီအလေ့အကျင့်တွေက real-world project တည်ဆောက်ရာမှာ တိုက်ရိုက်အသုံးဝင်ပါလိမ့်မယ်။ Task တွေကို အစဉ်လိုက် အခက်တက်မြင့်တက်အောင် စီထားတာမို့ အရင် task ရဲ့ solution ကို နောက် task မှာ reuse လုပ်နိုင်ပါတယ်။

လေ့ကျင့်ခန်းများ

(1) `CounterService` တစ်ခု ဆောက်ပြီး BehaviorSubject<number> နဲ့ state ကို hold ထားကာ increment()/decrement()/reset() method သုံးခု implement လုပ်ပါ — component နှစ်ခုက ဒီ service တစ်ခုတည်းကို share ပြီး sync ဖြစ်အောင် စမ်းကြည့်ပါ။ (2) SignupForm reactive form တစ်ခု ဆောက်ပြီး email field မှာ Validators.email, password field မှာ Validators.minLength(6) ထည့်ပါ — invalid ဖြစ်ရင် error message ကို template မှာ ပြပါ။ (3) HttpClient နဲ့ public API (ဥပမာ JSONPlaceholder) ကနေ user list ကို fetch လုပ်ပြီး RxJS `map` operator သုံးပြီး name field တွေကိုပဲ array အနေနဲ့ ဖယ်ထုတ်ကာ `filter` operator နဲ့ character 5 လုံးထက်ရှည်တဲ့ name တွေပဲ ပြပါ။

Code နမူနာ

typescript
// counter.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CounterService {
  private countSubject = new BehaviorSubject<number>(0);
  count$ = this.countSubject.asObservable();

  increment() { this.countSubject.next(this.countSubject.value + 1); }
  decrement() { this.countSubject.next(this.countSubject.value - 1); }
  reset() { this.countSubject.next(0); }
}

// user-list.component.ts (task 3 skeleton)
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, filter } from 'rxjs/operators';

interface User { name: string; }

@Component({ selector: 'app-user-list', template: `<li *ngFor="let n of names$ | async">{{ n }}</li>` })
export class UserListComponent implements OnInit {
  names$ = this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
    map(users => users.map(u => u.name)),
    map(names => names.filter(n => n.length > 5))
  );

  constructor(private http: HttpClient) {}
  ngOnInit() {}
}
You should see
Counter service ကို component နှစ်ခုက share သုံးရင် တစ်ခုမှာ increment နှိပ်တာနဲ့ တခြား component ရဲ့ display ပါ ချက်ချင်းပြောင်းသွားမှာဖြစ်ပြီး signup form မှာ invalid input ရိုက်ရင် error message ချက်ချင်းပေါ်ကာ user list မှာ character 5 လုံးထက်ရှည်တဲ့ name တွေပဲ ကျန်ပြသည်။

Real-world usage

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

Exercise တစ်ခုစီမှာ typed service, validated form, operator pipeline နဲ့ tests ပါစေ။ Rapid input, server error, retry policy နဲ့ navigation cleanup ကိုစမ်းပါ။

Mini challenge

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

Exercise Set 2: Services, Forms & RxJS နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

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

@Injectable({ providedIn: 'root' })
export class CounterService {
  private countSubject = new BehaviorSubject<number>(0);
  count$ = this.countSubject.asObservable();

  increment() { this.countSubject.next(this.countSubject.value + 1); }
  decrement() { this.countSubject.next(this.countSubject.value - 1); }
  reset() { this.countSubject.next(0); }
}

// user-list.component.ts (task 3 skeleton)
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, filter } from 'rxjs/operators';

interface User { name: string; }

@Component({ selector: 'app-user-list', template: `<li *ngFor="let n of names$ | async">{{ n }}</li>` })
export class UserListComponent implements OnInit {
  names$ = this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
    map(users => users.map(u => u.name)),
    map(names => names.filter(n => n.length > 5))
  );

  constructor(private http: HttpClient) {}
  ngOnInit() {}
}

ရလဒ်: Exercise Set 2: Services, Forms & RxJS ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

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

Exercise တစ်ခုစီမှာ typed service, validated form, operator pipeline နဲ့ tests ပါစေ။ Rapid input, server error, retry policy နဲ့ navigation cleanup ကိုစမ်းပါ။

Quick Quiz 1 / 3

Exercise Set 2: Services, Forms & RxJS ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • လိုအပ်ချက် → စဉ်းစားပြီး implement → စမ်းသပ်ပြီးရှင်းပြ
  • ဒီ exercise set က service/DI boundaries, form contracts နဲ့ RxJS async behavior ကိုပေါင်းစပ်စမ်းပါတယ်။ Dependency diagram, form state machine နဲ့ stream timeline ကိုအရင်ရေးပြီး success/error/cancellation/cleanup behavior သတ်မှတ်ပါ။ HTTP နှင့် time dependencies ကိုtest doubles ဖြင့်control လုပ်ပြီး user-observable results ကိုစစ်ပါ။
  • Happy path subscription တစ်ခုသာရေးပြီး cancellation/error/teardown မစမ်းလျှင် production race conditions ကိုမတွေ့နိုင်ပါ။
  • Exercise တစ်ခုစီမှာ typed service, validated form, operator pipeline နဲ့ tests ပါစေ။ Rapid input, server error, retry policy နဲ့ navigation cleanup ကိုစမ်းပါ။

Angular — Overview — Angular

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

  • Happy path subscription တစ်ခုသာရေးပြီး cancellation/error/teardown မစမ်းလျှင် production race conditions ကိုမတွေ့နိုင်ပါ။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

Exercise Set 2: Services, Forms & RxJS နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Counter service ကို component နှစ်ခုက share သုံးရင် တစ်ခုမှာ increment နှိပ်တာနဲ့ တခြား component ရဲ့ display ပါ ချက်ချင်းပြောင်းသွားမှာဖြစ်ပြီး signup form မှာ invalid input ရိုက်ရင် error message ချက်ချင်းပေါ်ကာ user list မှာ character 5 လုံးထက်ရှည်တဲ့ name တွေပဲ ကျန်ပြသည်။

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

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

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

Exercise Set 2: Services, Forms & RxJS | Thuta Learning