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

@Input() - Parent to Child

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

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

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

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

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

@Input() decorator သည် parent component မှ child component သို့ data pass လုပ်ရန် အသုံးပြုသည်။

Problem → Why → Solution

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

Child ထဲ input object ကိုတိုက်ရိုက်ပြင်ခြင်းက parent state ကိုမမျှော်လင့်ဘဲပြောင်းစေပါတယ်။

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

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

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

Input က parent-owned data ကို child ဆီ one-way ပို့တဲ့ public API ဖြစ်ပါတယ်။ Required/optional contract, default value, transform နှင့် immutable update policy ကိုသတ်မှတ်ပြီး child က input ကိုကိုယ်ပိုင် source of truth လို mutate မလုပ်သင့်ပါ။ Object reference မပြောင်းဘဲ nested value ပြင်ခြင်းက optimized change detection တွင် update မမြင်နိုင်တာကြောင့် new reference pattern သုံးပါ။

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

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

အဓိက Syntax / Pattern

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

@Component({
  selector: 'app-child',
  template: `
    <div class="child">
      <h3>{{ title }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ChildComponent {
  @Input() title: string = '';
  @Input() age: number = 0;
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child [title]="userName" [age]="userAge"></app-child>
  `
})
export class ParentComponent {
  userName = 'John Doe';
  userAge = 25;
}

Input က parent-owned data ကို child ဆီ one-way ပို့တဲ့ public API ဖြစ်ပါတယ်။ Required/optional contract, default value, transform နှင့် immutable update policy ကိုသတ်မှတ်ပြီး child က input ကိုကိုယ်ပိုင် source of truth လို mutate မလုပ်သင့်ပါ။ Object reference မပြောင်းဘဲ nested value ပြင်ခြင်းက optimized change detection တွင် update မမြင်နိုင်တာကြောင့် new reference pattern သုံးပါ။

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

  1. User input// child.component.ts

    @Input() decorator သည် parent component မှ child component သို့ data pass လုပ်ရန် အသုံးပြုသည်။

  2. Binding + validation@Input() age: number = 0;

    Input က parent-owned data ကို child ဆီ one-way ပို့တဲ့ public API ဖြစ်ပါတယ်။ Required/optional contract, default value, transform နှင့် immutable update policy ကိုသတ်မှတ်ပြီး child က input ကိုကိုယ်ပိုင် source of truth လို mutate မလုပ်သင့်ပါ။ Object reference မပြောင်းဘဲ nested value ပြင်ခြင်းက optimized change detection တွင် update မမြင်နိုင်တာကြောင့် new reference pattern သုံးပါ။

  3. Valid state / feedback}

    Reusable product card အတွက် required product နဲ့ optional compact mode input ရေးပြီး parent က immutable update လုပ်ချိန် UI ပြောင်းကြောင်းစစ်ပါ။

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

Success

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

Failure

Child ထဲ input object ကိုတိုက်ရိုက်ပြင်ခြင်းက parent state ကိုမမျှော်လင့်ဘဲပြောင်းစေပါတယ်။

@Input() decorator သည် parent component မှ child component သို့ data pass လုပ်ရန် အသုံးပြုသည်။

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

@Component({
  selector: 'app-child',
  template: `
    <div class="child">
      <h3>{{ title }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ChildComponent {
  @Input() title: string = '';
  @Input() age: number = 0;
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child [title]="userName" [age]="userAge"></app-child>
  `
})
export class ParentComponent {
  userName = 'John Doe';
  userAge = 25;
}
You should see
(Child component သည် parent မှ 'John Doe' နှင့် 25 ကို receive လုပ်ပြီး display ပြသမည်)

Real-world usage

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

Reusable product card အတွက် required product နဲ့ optional compact mode input ရေးပြီး parent က immutable update လုပ်ချိန် UI ပြောင်းကြောင်းစစ်ပါ။

Mini challenge

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

@Input() - Parent to Child နမူနာကို 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, Input } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <div class="child">
      <h3>{{ title }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ChildComponent {
  @Input() title: string = '';
  @Input() age: number = 0;
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child [title]="userName" [age]="userAge"></app-child>
  `
})
export class ParentComponent {
  userName = 'John Doe';
  userAge = 25;
}

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

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

Reusable product card အတွက် required product နဲ့ optional compact mode input ရေးပြီး parent က immutable update လုပ်ချိန် UI ပြောင်းကြောင်းစစ်ပါ။

Quick Quiz 1 / 3

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

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • User input → Binding + validation → Valid state / feedback
  • Input က parent-owned data ကို child ဆီ one-way ပို့တဲ့ public API ဖြစ်ပါတယ်။ Required/optional contract, default value, transform နှင့် immutable update policy ကိုသတ်မှတ်ပြီး child က input ကိုကိုယ်ပိုင် source of truth လို mutate မလုပ်သင့်ပါ။ Object reference မပြောင်းဘဲ nested value ပြင်ခြင်းက optimized change detection တွင် update မမြင်နိုင်တာကြောင့် new reference pattern သုံးပါ။
  • Child ထဲ input object ကိုတိုက်ရိုက်ပြင်ခြင်းက parent state ကိုမမျှော်လင့်ဘဲပြောင်းစေပါတယ်။
  • Reusable product card အတွက် required product နဲ့ optional compact mode input ရေးပြီး parent က immutable update လုပ်ချိန် UI ပြောင်းကြောင်းစစ်ပါ။

Angular — Components — Angular

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

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

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Child component သည် parent မှ 'John Doe' နှင့် 25 ကို receive လုပ်ပြီး display ပြသမည်)

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

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

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

@Input() - Parent to Child | Thuta Learning