ရိုးရိုးလေး ရှင်းပြမယ်
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 ကိုစစ်ပါ။
- လိုအပ်ချက်
- စဉ်းစားပြီး implement
- စမ်းသပ်ပြီးရှင်းပြ
Exercise Set 2: Services, Forms & RxJS ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// 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 ကို အဆင့်လိုက်ဖတ်မယ်
- လိုအပ်ချက်
// 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 လုပ်နိုင်ပါတယ်။
- စဉ်းစားပြီး implement
import { 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 ကိုစစ်ပါ။
- စမ်းသပ်ပြီးရှင်းပြ
}Exercise တစ်ခုစီမှာ typed service, validated form, operator pipeline နဲ့ tests ပါစေ။ Rapid input, server error, retry policy နဲ့ navigation cleanup ကိုစမ်းပါ။
- လိုအပ်ချက်
- စဉ်းစားပြီး implement
- စမ်းသပ်ပြီးရှင်းပြ
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 နမူနာ
// 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() {}
}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 ကိုခန့်မှန်းပါ
// 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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- လိုအပ်ချက် → စဉ်းစားပြီး 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