ရိုးရိုးလေး ရှင်းပြမယ်
ViewChild & ViewChildren ကို ရိုးရိုးလေးနားလည်မယ်
@ViewChild သည် parent component တွင် child component သို့မဟုတ် DOM element ကို access လုပ်ရန် အသုံးပြုသည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Parent က child methods အများအပြားကို ViewChild နဲ့ခေါ်ခြင်းက component encapsulation ကိုပျက်စေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
View queries က component ၏ own rendered view ထဲရှိ child instance, directive သို့ template reference ကိုရယူပေးပါတယ်။ Result က view initialization timing နဲ့ conditional rendering ပေါ်မူတည်တာကြောင့် lifecycle/query semantics ကိုဂရုစိုက်ရပါတယ်။ Parent-child communication ကို inputs/outputs ဖြင့်ဖြေရှင်းနိုင်လျှင် query ထက်ပိုရှင်းပြီး ViewChild ကို focus, measurement သို့ imperative third-party API လိုကိစ္စများအတွက်ကန့်သတ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
ViewChild & ViewChildren ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// child.component.ts
export class ChildComponent {
message = 'Hello from Child!';
showAlert() {
alert('Child method called!');
}
}
// parent.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child></app-child>
<button (click)="callChildMethod()">Call Child</button>
`
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child!: ChildComponent;
ngAfterViewInit() {
console.log(this.child.message);
}
callChildMethod() {
this.child.showAlert();
}
}View queries က component ၏ own rendered view ထဲရှိ child instance, directive သို့ template reference ကိုရယူပေးပါတယ်။ Result က view initialization timing နဲ့ conditional rendering ပေါ်မူတည်တာကြောင့် lifecycle/query semantics ကိုဂရုစိုက်ရပါတယ်။ Parent-child communication ကို inputs/outputs ဖြင့်ဖြေရှင်းနိုင်လျှင် query ထက်ပိုရှင်းပြီး ViewChild ကို focus, measurement သို့ imperative third-party API လိုကိစ္စများအတွက်ကန့်သတ်ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
// child.component.ts@ViewChild သည် parent component တွင် child component သို့မဟုတ် DOM element ကို access လုပ်ရန် အသုံးပြုသည်။
- Component + template
<app-child></app-child>View queries က component ၏ own rendered view ထဲရှိ child instance, directive သို့ template reference ကိုရယူပေးပါတယ်။ Result က view initialization timing နဲ့ conditional rendering ပေါ်မူတည်တာကြောင့် lifecycle/query semantics ကိုဂရုစိုက်ရပါတယ်။ Parent-child communication ကို inputs/outputs ဖြင့်ဖြေရှင်းနိုင်လျှင် query ထက်ပိုရှင်းပြီး ViewChild ကို focus, measurement သို့ imperative third-party API လိုကိစ္စများအတွက်ကန့်သတ်ပါ။
- Rendered Angular UI
}Dialog ဖွင့်ချိန် input ကို focus လုပ်ရန် ViewChild သုံးပြီး conditional render မတိုင်မီ/ပြီးနောက် query value ကိုစမ်းပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
ViewChild & ViewChildren ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Parent က child methods အများအပြားကို ViewChild နဲ့ခေါ်ခြင်းက component encapsulation ကိုပျက်စေပါတယ်။
@ViewChild သည် parent component တွင် child component သို့မဟုတ် DOM element ကို access လုပ်ရန် အသုံးပြုသည်။
// child.component.ts
export class ChildComponent {
message = 'Hello from Child!';
showAlert() {
alert('Child method called!');
}
}
// parent.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child></app-child>
<button (click)="callChildMethod()">Call Child</button>
`
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child!: ChildComponent;
ngAfterViewInit() {
console.log(this.child.message);
}
callChildMethod() {
this.child.showAlert();
}
}(Parent သည် child component ၏ properties နှင့် methods များကို တိုက်ရိုက် access လုပ်နိုင်သည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Dialog ဖွင့်ချိန် input ကို focus လုပ်ရန် ViewChild သုံးပြီး conditional render မတိုင်မီ/ပြီးနောက် query value ကိုစမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
ViewChild & ViewChildren နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// child.component.ts
export class ChildComponent {
message = 'Hello from Child!';
showAlert() {
alert('Child method called!');
}
}
// parent.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child></app-child>
<button (click)="callChildMethod()">Call Child</button>
`
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child!: ChildComponent;
ngAfterViewInit() {
console.log(this.child.message);
}
callChildMethod() {
this.child.showAlert();
}
}ရလဒ်: ViewChild & ViewChildren ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Dialog ဖွင့်ချိန် input ကို focus လုပ်ရန် ViewChild သုံးပြီး conditional render မတိုင်မီ/ပြီးနောက် query value ကိုစမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- View queries က component ၏ own rendered view ထဲရှိ child instance, directive သို့ template reference ကိုရယူပေးပါတယ်။ Result က view initialization timing နဲ့ conditional rendering ပေါ်မူတည်တာကြောင့် lifecycle/query semantics ကိုဂရုစိုက်ရပါတယ်။ Parent-child communication ကို inputs/outputs ဖြင့်ဖြေရှင်းနိုင်လျှင် query ထက်ပိုရှင်းပြီး ViewChild ကို focus, measurement သို့ imperative third-party API လိုကိစ္စများအတွက်ကန့်သတ်ပါ။
- Parent က child methods အများအပြားကို ViewChild နဲ့ခေါ်ခြင်းက component encapsulation ကိုပျက်စေပါတယ်။
- Dialog ဖွင့်ချိန် input ကို focus လုပ်ရန် ViewChild သုံးပြီး conditional render မတိုင်မီ/ပြီးနောက် query value ကိုစမ်းပါ။
Angular — Components — Angular