ရိုးရိုးလေး ရှင်းပြမယ်
Change Detection ကို ရိုးရိုးလေးနားလည်မယ်
Change Detection သည် Angular မှ component data changes များကို detect လုပ်ပြီး view ကို update လုပ်သည့် mechanism ဖြစ်သည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
`detectChanges()` ကိုနေရာတိုင်းခေါ်ပြီး symptom ဖုံးခြင်းက actual state ownership/timing bug ကိုပိုခက်စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Change detection က application state ပြောင်းမှုကို template bindings ဆီပြန်တွက်ပြီး DOM update ဆုံးဖြတ်တဲ့စနစ်ဖြစ်ပါတယ်။ OnPush/optimized strategy, immutable references, signals/observables နဲ့ stable list tracking တို့က check scope နဲ့ predictability ကောင်းစေပါတယ်။ Performance ကိုခန့်မှန်းပြီးမပြင်ဘဲ profiler ဖြင့်တိုင်းကာ template heavy work, broad mutable state နဲ့ unnecessary emissions ကိုရှာပါ။
- Input + state
- Component + template
- Rendered Angular UI
Change Detection ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
// Default Change Detection (checks all the time)
@Component({
selector: 'app-default',
template: `<p>{{ data }}</p>`
})
export class DefaultComponent {
data = 'Hello';
}
// OnPush Change Detection (checks only when @Input changes)
@Component({
selector: 'app-optimized',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data: any;
constructor(private cdr: ChangeDetectorRef) {}
// Manually trigger change detection if needed
updateData() {
this.data = 'New value';
this.cdr.markForCheck(); // Tell Angular to check
}
}Change detection က application state ပြောင်းမှုကို template bindings ဆီပြန်တွက်ပြီး DOM update ဆုံးဖြတ်တဲ့စနစ်ဖြစ်ပါတယ်။ OnPush/optimized strategy, immutable references, signals/observables နဲ့ stable list tracking တို့က check scope နဲ့ predictability ကောင်းစေပါတယ်။ Performance ကိုခန့်မှန်းပြီးမပြင်ဘဲ profiler ဖြင့်တိုင်းကာ template heavy work, broad mutable state နဲ့ unnecessary emissions ကိုရှာပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
import { ChangeDetectionStrategy, ChangeDetectorRef }...Change Detection သည် Angular မှ component data changes များကို detect လုပ်ပြီး view ကို update လုပ်သည့် mechanism ဖြစ်သည်။
- Component + template
template: `<p>{{ data }}</p>`,Change detection က application state ပြောင်းမှုကို template bindings ဆီပြန်တွက်ပြီး DOM update ဆုံးဖြတ်တဲ့စနစ်ဖြစ်ပါတယ်။ OnPush/optimized strategy, immutable references, signals/observables နဲ့ stable list tracking တို့က check scope နဲ့ predictability ကောင်းစေပါတယ်။ Performance ကိုခန့်မှန်းပြီးမပြင်ဘဲ profiler ဖြင့်တိုင်းကာ template heavy work, broad mutable state နဲ့ unnecessary emissions ကိုရှာပါ။
- Rendered Angular UI
}OnPush child နဲ့ object input တစ်ခုရေးပြီး nested mutation နှင့် immutable replacement နှစ်မျိုး UI result ကိုနှိုင်းပါ။ Computed derived state လည်းထည့်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Change Detection ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
`detectChanges()` ကိုနေရာတိုင်းခေါ်ပြီး symptom ဖုံးခြင်းက actual state ownership/timing bug ကိုပိုခက်စေပါတယ်။
Change Detection သည် Angular မှ component data changes များကို detect လုပ်ပြီး view ကို update လုပ်သည့် mechanism ဖြစ်သည်။
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
// Default Change Detection (checks all the time)
@Component({
selector: 'app-default',
template: `<p>{{ data }}</p>`
})
export class DefaultComponent {
data = 'Hello';
}
// OnPush Change Detection (checks only when @Input changes)
@Component({
selector: 'app-optimized',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data: any;
constructor(private cdr: ChangeDetectorRef) {}
// Manually trigger change detection if needed
updateData() {
this.data = 'New value';
this.cdr.markForCheck(); // Tell Angular to check
}
}(OnPush strategy သုံးခြင်းဖြင့် performance ကောင်းလာပြီး unnecessary checks လျှော့ချနိုင်သည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
OnPush child နဲ့ object input တစ်ခုရေးပြီး nested mutation နှင့် immutable replacement နှစ်မျိုး UI result ကိုနှိုင်းပါ။ Computed derived state လည်းထည့်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Change Detection နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
// Default Change Detection (checks all the time)
@Component({
selector: 'app-default',
template: `<p>{{ data }}</p>`
})
export class DefaultComponent {
data = 'Hello';
}
// OnPush Change Detection (checks only when @Input changes)
@Component({
selector: 'app-optimized',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data: any;
constructor(private cdr: ChangeDetectorRef) {}
// Manually trigger change detection if needed
updateData() {
this.data = 'New value';
this.cdr.markForCheck(); // Tell Angular to check
}
}ရလဒ်: Change Detection ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
OnPush child နဲ့ object input တစ်ခုရေးပြီး nested mutation နှင့် immutable replacement နှစ်မျိုး UI result ကိုနှိုင်းပါ။ Computed derived state လည်းထည့်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Change detection က application state ပြောင်းမှုကို template bindings ဆီပြန်တွက်ပြီး DOM update ဆုံးဖြတ်တဲ့စနစ်ဖြစ်ပါတယ်။ OnPush/optimized strategy, immutable references, signals/observables နဲ့ stable list tracking တို့က check scope နဲ့ predictability ကောင်းစေပါတယ်။ Performance ကိုခန့်မှန်းပြီးမပြင်ဘဲ profiler ဖြင့်တိုင်းကာ template heavy work, broad mutable state နဲ့ unnecessary emissions ကိုရှာပါ။
- `detectChanges()` ကိုနေရာတိုင်းခေါ်ပြီး symptom ဖုံးခြင်းက actual state ownership/timing bug ကိုပိုခက်စေပါတယ်။
- OnPush child နဲ့ object input တစ်ခုရေးပြီး nested mutation နှင့် immutable replacement နှစ်မျိုး UI result ကိုနှိုင်းပါ။ Computed derived state လည်းထည့်ပါ။
Angular — Signals — Angular