Thuta Learning
ရှာဖွေရန်
ExercisesWeb Developmentintermediate

Exercise Set 2: Services, Forms & RxJS

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

  • Exercise Set 2: Services, Forms & RxJS ကို ကိုယ်တိုင် လေ့ကျင့်ကြည့်မယ်
  • သင်ခဲ့ပြီးသား skill တွေကို practice လုပ်ပြီး ခိုင်မာအောင်လုပ်မယ်
  • အမှားရှာ၊ ပြင်တတ်၊ ကိုယ်တိုင် check လုပ်တတ်မယ်

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

ဒီ 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 တွေပဲ ကျန်ပြသည်။

၅ မိနစ် စမ်းကြည့်

5 minutes အတွင်း task (1) ကို ချဲ့ပြီး count$ Observable ကို template ထဲမှာ async pipe နဲ့ တိုက်ရိုက် display လုပ်ကာ manual subscribe() လုံးဝမလိုအောင် ပြောင်းရေးကြည့်ပါ။

သတိလေးတစ်ချက်

Public demo API တွေဟာ rate limit ရှိနိုင်တဲ့အတွက် request အများကြီး frequent ခေါ်မိရင် error response ပြန်လာနိုင်ကြောင်း သတိထားပြီး RxJS operator chain ကို component logic ထဲမှာ မဟုတ်ဘဲ service layer မှာ ထားလေ့ရှိပါတယ်။

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

  • HttpClientModule ကို app.module.ts ထဲ import မလုပ်ဘဲ HttpClient ကို inject လုပ်ကြည့်ပြီး error တက်တတ်သည်
  • Reactive form ရဲ့ FormControl valid/invalid state ကို check ရာမှာ touched/dirty condition ထည့်မစဉ်းစားဘဲ page load တိုင်း error message ချက်ချင်းပြပြီး UX ညံ့တတ်သည်

အခု ကိုယ်တိုင် စမ်းကြည့်

5 minutes အတွင်း task (1) ကို ချဲ့ပြီး count$ Observable ကို template ထဲမှာ async pipe နဲ့ တိုက်ရိုက် display လုပ်ကာ manual subscribe() လုံးဝမလိုအောင် ပြောင်းရေးကြည့်ပါ။

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

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