Thuta Learning
Angular
IntermediateWeb Developmentအလယ်အလတ်

@Output() - Child to Parent

Angularသင်ခန်းစာ 1920 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • @Output() - Child to Parent ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

@Output() - Child to Parent ကို ရိုးရိုးလေးနားလည်မယ်

@Output() နှင့် EventEmitter သည် child component မှ parent component သို့ events/data pass လုပ်ရန် အသုံးပြုသည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

State object အားလုံးကို output payload အဖြစ်ပို့ခြင်းက components များကိုတင်းကျပ်စွာချိတ်ဆက်စေပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Output က child မှ parent ဆီ event အဖြစ် intent သို့ result ပို့တဲ့ contract ဖြစ်ပြီး parent data ကို child ကတိုက်ရိုက်မပြင်စေပါ။ Event payload ကို minimal, typed နဲ့ domain-specific ထားပြီး native DOM event နာမည်နဲ့မရှုပ်စေပါ။ Output ကို command bus လိုအလွန်သုံးမယ့်အစား sibling/global coordination ကို service/state layer ထဲရွှေ့ပါ။

Mental Model
  1. User input
  2. Binding + validation
  3. Valid state / feedback

@Output() - Child to Parent ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// child.component.ts
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <button (click)="sendData()">Send to Parent</button>
  `
})
export class ChildComponent {
  @Output() dataEvent = new EventEmitter<string>();
  
  sendData() {
    this.dataEvent.emit('Hello from Child!');
  }
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child (dataEvent)="receiveData($event)"></app-child>
    <p>{{ message }}</p>
  `
})
export class ParentComponent {
  message = '';
  
  receiveData(data: string) {
    this.message = data;
  }
}

Output က child မှ parent ဆီ event အဖြစ် intent သို့ result ပို့တဲ့ contract ဖြစ်ပြီး parent data ကို child ကတိုက်ရိုက်မပြင်စေပါ။ Event payload ကို minimal, typed နဲ့ domain-specific ထားပြီး native DOM event နာမည်နဲ့မရှုပ်စေပါ။ Output ကို command bus လိုအလွန်သုံးမယ့်အစား sibling/global coordination ကို service/state layer ထဲရွှေ့ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. User input// child.component.ts

    @Output() နှင့် EventEmitter သည် child component မှ parent component သို့ events/data pass လုပ်ရန် အသုံးပြုသည်။

  2. Binding + validation// parent.component.ts

    Output က child မှ parent ဆီ event အဖြစ် intent သို့ result ပို့တဲ့ contract ဖြစ်ပြီး parent data ကို child ကတိုက်ရိုက်မပြင်စေပါ။ Event payload ကို minimal, typed နဲ့ domain-specific ထားပြီး native DOM event နာမည်နဲ့မရှုပ်စေပါ။ Output ကို command bus လိုအလွန်သုံးမယ့်အစား sibling/global coordination ကို service/state layer ထဲရွှေ့ပါ။

  3. Valid state / feedback}

    Task row က toggle/delete intent နှစ်ခု emit လုပ်ပြီး parent က immutable list update လုပ်အောင်ရေးပါ။ Payload မှာလိုအပ်တဲ့ ID သာပို့ပါ။

Execution Flow
  1. User input
  2. Binding + validation
  3. Valid state / feedback

Success

@Output() - Child to Parent ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

State object အားလုံးကို output payload အဖြစ်ပို့ခြင်းက components များကိုတင်းကျပ်စွာချိတ်ဆက်စေပါတယ်။

@Output() နှင့် EventEmitter သည် child component မှ parent component သို့ events/data pass လုပ်ရန် အသုံးပြုသည်။

typescript
// child.component.ts
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <button (click)="sendData()">Send to Parent</button>
  `
})
export class ChildComponent {
  @Output() dataEvent = new EventEmitter<string>();
  
  sendData() {
    this.dataEvent.emit('Hello from Child!');
  }
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child (dataEvent)="receiveData($event)"></app-child>
    <p>{{ message }}</p>
  `
})
export class ParentComponent {
  message = '';
  
  receiveData(data: string) {
    this.message = data;
  }
}
You should see
(Child component ၏ button ကိုနှိပ်လျှင် parent component တွင် 'Hello from Child!' ဟု ပေါ်လာမည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Task row က toggle/delete intent နှစ်ခု emit လုပ်ပြီး parent က immutable list update လုပ်အောင်ရေးပါ။ Payload မှာလိုအပ်တဲ့ ID သာပို့ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

@Output() - Child to Parent နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// child.component.ts
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <button (click)="sendData()">Send to Parent</button>
  `
})
export class ChildComponent {
  @Output() dataEvent = new EventEmitter<string>();
  
  sendData() {
    this.dataEvent.emit('Hello from Child!');
  }
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child (dataEvent)="receiveData($event)"></app-child>
    <p>{{ message }}</p>
  `
})
export class ParentComponent {
  message = '';
  
  receiveData(data: string) {
    this.message = data;
  }
}

ရလဒ်: @Output() - Child to Parent ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Task row က toggle/delete intent နှစ်ခု emit လုပ်ပြီး parent က immutable list update လုပ်အောင်ရေးပါ။ Payload မှာလိုအပ်တဲ့ ID သာပို့ပါ။

Quick Quiz 1 / 3

@Output() - Child to Parent ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • User input → Binding + validation → Valid state / feedback
  • Output က child မှ parent ဆီ event အဖြစ် intent သို့ result ပို့တဲ့ contract ဖြစ်ပြီး parent data ကို child ကတိုက်ရိုက်မပြင်စေပါ။ Event payload ကို minimal, typed နဲ့ domain-specific ထားပြီး native DOM event နာမည်နဲ့မရှုပ်စေပါ။ Output ကို command bus လိုအလွန်သုံးမယ့်အစား sibling/global coordination ကို service/state layer ထဲရွှေ့ပါ။
  • State object အားလုံးကို output payload အဖြစ်ပို့ခြင်းက components များကိုတင်းကျပ်စွာချိတ်ဆက်စေပါတယ်။
  • Task row က toggle/delete intent နှစ်ခု emit လုပ်ပြီး parent က immutable list update လုပ်အောင်ရေးပါ။ Payload မှာလိုအပ်တဲ့ ID သာပို့ပါ။

Angular — Components — Angular

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • State object အားလုံးကို output payload အဖြစ်ပို့ခြင်းက components များကိုတင်းကျပ်စွာချိတ်ဆက်စေပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

@Output() - Child to Parent နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Child component ၏ button ကိုနှိပ်လျှင် parent component တွင် 'Hello from Child!' ဟု ပေါ်လာမည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

@Output() - Child to Parent | Thuta Learning