ရိုးရိုးလေး ရှင်းပြမယ်
Custom Pipes ကို ရိုးရိုးလေးနားလည်မယ်
Application ၏ specific requirements များအတွက် custom pipes များ ဖန်တီးနိုင်သည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Impure pipe ဖြင့် list filter လုပ်ခြင်းက change detection တစ်ခါတိုင်း run ပြီး performance ပြဿနာဖြစ်စေနိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Custom pipe ကို presentation-only, deterministic transformation အတွက်သုံးပြီး filtering business logic သို့ side effects မထည့်သင့်ပါ။ Pure pipe မှာ arguments တူလျှင် result တူရပြီး expensive computation အတွက် data model/computed state ကိုစဉ်းစားပါ။ Pipe name, input/output types, null behavior နဲ့ locale assumptions ကို public API လိုသတ်မှတ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Custom Pipes ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
// exponential.pipe.ts
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent: number = 1): number {
return Math.pow(value, exponent);
}
}
// Usage in template
<p>{{ 'Angular' | reverse }}</p> <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p> <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p> <!-- 25 (5^2) -->
// Don't forget to declare pipes in module
@NgModule({
declarations: [ReversePipe, ExponentialPipe]
})Custom pipe ကို presentation-only, deterministic transformation အတွက်သုံးပြီး filtering business logic သို့ side effects မထည့်သင့်ပါ။ Pure pipe မှာ arguments တူလျှင် result တူရပြီး expensive computation အတွက် data model/computed state ကိုစဉ်းစားပါ။ Pipe name, input/output types, null behavior နဲ့ locale assumptions ကို public API လိုသတ်မှတ်ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
// reverse.pipe.tsApplication ၏ specific requirements များအတွက် custom pipes များ ဖန်တီးနိုင်သည်။
- Component + template
})Custom pipe ကို presentation-only, deterministic transformation အတွက်သုံးပြီး filtering business logic သို့ side effects မထည့်သင့်ပါ။ Pure pipe မှာ arguments တူလျှင် result တူရပြီး expensive computation အတွက် data model/computed state ကိုစဉ်းစားပါ။ Pipe name, input/output types, null behavior နဲ့ locale assumptions ကို public API လိုသတ်မှတ်ပါ။
- Rendered Angular UI
})File-size formatting pipe တစ်ခုရေးပြီး bytes, KB, MB, zero, negative နဲ့ null cases ကိုunit test လုပ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Custom Pipes ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Impure pipe ဖြင့် list filter လုပ်ခြင်းက change detection တစ်ခါတိုင်း run ပြီး performance ပြဿနာဖြစ်စေနိုင်ပါတယ်။
Application ၏ specific requirements များအတွက် custom pipes များ ဖန်တီးနိုင်သည်။
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
// exponential.pipe.ts
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent: number = 1): number {
return Math.pow(value, exponent);
}
}
// Usage in template
<p>{{ 'Angular' | reverse }}</p> <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p> <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p> <!-- 25 (5^2) -->
// Don't forget to declare pipes in module
@NgModule({
declarations: [ReversePipe, ExponentialPipe]
})(Custom pipes များအလုပ်လုပ်ပြီး data များကို transform လုပ်မည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
File-size formatting pipe တစ်ခုရေးပြီး bytes, KB, MB, zero, negative နဲ့ null cases ကိုunit test လုပ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Custom Pipes နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'reverse'
})
export class ReversePipe implements PipeTransform {
transform(value: string): string {
return value.split('').reverse().join('');
}
}
// exponential.pipe.ts
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent: number = 1): number {
return Math.pow(value, exponent);
}
}
// Usage in template
<p>{{ 'Angular' | reverse }}</p> <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p> <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p> <!-- 25 (5^2) -->
// Don't forget to declare pipes in module
@NgModule({
declarations: [ReversePipe, ExponentialPipe]
})ရလဒ်: Custom Pipes ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
File-size formatting pipe တစ်ခုရေးပြီး bytes, KB, MB, zero, negative နဲ့ null cases ကိုunit test လုပ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Custom pipe ကို presentation-only, deterministic transformation အတွက်သုံးပြီး filtering business logic သို့ side effects မထည့်သင့်ပါ။ Pure pipe မှာ arguments တူလျှင် result တူရပြီး expensive computation အတွက် data model/computed state ကိုစဉ်းစားပါ။ Pipe name, input/output types, null behavior နဲ့ locale assumptions ကို public API လိုသတ်မှတ်ပါ။
- Impure pipe ဖြင့် list filter လုပ်ခြင်းက change detection တစ်ခါတိုင်း run ပြီး performance ပြဿနာဖြစ်စေနိုင်ပါတယ်။
- File-size formatting pipe တစ်ခုရေးပြီး bytes, KB, MB, zero, negative နဲ့ null cases ကိုunit test လုပ်ပါ။
Angular — Templates — Angular