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

ViewChild & ViewChildren

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

@ViewChild သည် parent component တွင် child component သို့မဟုတ် DOM element ကို access လုပ်ရန် အသုံးပြုသည်။

typescript
// 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();
  }
}
You should see
(Parent သည် child component ၏ properties နှင့် methods များကို တိုက်ရိုက် access လုပ်နိုင်သည်)
ViewChild & ViewChildren | Thuta Learning