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

Two-Way Binding [( )]

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

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

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

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

Two-Way Binding [( )] ကို ရိုးရိုးလေးနားလည်မယ်

Two-Way Data Binding [(...)] (Banana in a Box syntax) သည် property binding နှင့် event binding ကိုပေါင်းစပ်ထားသည်။ များသောအားဖြင့် form inputs များတွင်သုံးပြီး [(ngModel)] directive ကိုသုံးရသည်။

Problem → Why → Solution

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

Parent နဲ့ child နှစ်ဖက်လုံးက object တစ်ခုကိုတိတ်တဆိတ် mutate လုပ်ခြင်းက source of truth မသိတော့စေပါတယ်။

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

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

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

Two-way binding က property input နဲ့ change event output ကို `[(...)]` syntax ဖြင့်ပေါင်းထားခြင်းဖြစ်ပါတယ်။ Forms တွင် `[(ngModel)]` က view နဲ့ component state sync လုပ်သော်လည်း state ownership မရှင်းလျှင် hidden mutation ဖြစ်နိုင်ပါတယ်။ Reusable component အတွက် value/valueChange pair သို့ model API သဘောတရားကိုနားလည်ပြီး complex forms မှာ reactive forms ကိုစဉ်းစားပါ။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Two-Way Binding [( )] ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// app.module.ts
import { FormsModule } from '@angular/forms';

@NgModule({
  imports: [BrowserModule, FormsModule]
})

// component.html
<input [(ngModel)]="username" type="text">
<p>Hello, {{ username }}!</p>

// component.ts
export class MyComponent {
  username = 'World';
}

Two-way binding က property input နဲ့ change event output ကို `[(...)]` syntax ဖြင့်ပေါင်းထားခြင်းဖြစ်ပါတယ်။ Forms တွင် `[(ngModel)]` က view နဲ့ component state sync လုပ်သော်လည်း state ownership မရှင်းလျှင် hidden mutation ဖြစ်နိုင်ပါတယ်။ Reusable component အတွက် value/valueChange pair သို့ model API သဘောတရားကိုနားလည်ပြီး complex forms မှာ reactive forms ကိုစဉ်းစားပါ။

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

  1. Input + state// app.module.ts

    Two-Way Data Binding [(...)] (Banana in a Box syntax) သည် property binding နှင့် event binding ကိုပေါင်းစပ်ထားသည်။ များသောအားဖြင့် form inputs များတွင်သုံးပြီး [(ngModel)] directive ကိုသုံးရသည်။

  2. Component + template<input [(ngModel)]="username" type="text">

    Two-way binding က property input နဲ့ change event output ကို `[(...)]` syntax ဖြင့်ပေါင်းထားခြင်းဖြစ်ပါတယ်။ Forms တွင် `[(ngModel)]` က view နဲ့ component state sync လုပ်သော်လည်း state ownership မရှင်းလျှင် hidden mutation ဖြစ်နိုင်ပါတယ်။ Reusable component အတွက် value/valueChange pair သို့ model API သဘောတရားကိုနားလည်ပြီး complex forms မှာ reactive forms ကိုစဉ်းစားပါ။

  3. Rendered Angular UI}

    Search input တစ်ခုကို ngModel ဖြင့် bind လုပ်ပြီး trimmed query, clear action နဲ့ displayed model value တို့ကိုစမ်းပါ။ ထို့နောက် input/output pair နဲ့ custom control အယူအဆပြန်ရေးပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Success

Two-Way Binding [( )] ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Parent နဲ့ child နှစ်ဖက်လုံးက object တစ်ခုကိုတိတ်တဆိတ် mutate လုပ်ခြင်းက source of truth မသိတော့စေပါတယ်။

Two-Way Data Binding [(...)] (Banana in a Box syntax) သည် property binding နှင့် event binding ကိုပေါင်းစပ်ထားသည်။ များသောအားဖြင့် form inputs များတွင်သုံးပြီး [(ngModel)] directive ကိုသုံးရသည်။

⚠️ Important: FormsModule ကို import လုပ်ရမည်။

typescript
// app.module.ts
import { FormsModule } from '@angular/forms';

@NgModule({
  imports: [BrowserModule, FormsModule]
})

// component.html
<input [(ngModel)]="username" type="text">
<p>Hello, {{ username }}!</p>

// component.ts
export class MyComponent {
  username = 'World';
}
You should see
(Input box ထဲတွင် စာရိုက်လိုက်သည်နှင့်အမျှ အောက်ကစာသားသည် 'Hello, [typed text]!' ဟုချက်ချင်းပြောင်းလဲသွားမည်)

Real-world usage

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

Search input တစ်ခုကို ngModel ဖြင့် bind လုပ်ပြီး trimmed query, clear action နဲ့ displayed model value တို့ကိုစမ်းပါ။ ထို့နောက် input/output pair နဲ့ custom control အယူအဆပြန်ရေးပါ။

Mini challenge

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

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

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// app.module.ts
import { FormsModule } from '@angular/forms';

@NgModule({
  imports: [BrowserModule, FormsModule]
})

// component.html
<input [(ngModel)]="username" type="text">
<p>Hello, {{ username }}!</p>

// component.ts
export class MyComponent {
  username = 'World';
}

ရလဒ်: Two-Way Binding [( )] ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

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

Search input တစ်ခုကို ngModel ဖြင့် bind လုပ်ပြီး trimmed query, clear action နဲ့ displayed model value တို့ကိုစမ်းပါ။ ထို့နောက် input/output pair နဲ့ custom control အယူအဆပြန်ရေးပါ။

Quick Quiz 1 / 3

Two-Way Binding [( )] ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Input + state → Component + template → Rendered Angular UI
  • Two-way binding က property input နဲ့ change event output ကို `[(...)]` syntax ဖြင့်ပေါင်းထားခြင်းဖြစ်ပါတယ်။ Forms တွင် `[(ngModel)]` က view နဲ့ component state sync လုပ်သော်လည်း state ownership မရှင်းလျှင် hidden mutation ဖြစ်နိုင်ပါတယ်။ Reusable component အတွက် value/valueChange pair သို့ model API သဘောတရားကိုနားလည်ပြီး complex forms မှာ reactive forms ကိုစဉ်းစားပါ။
  • Parent နဲ့ child နှစ်ဖက်လုံးက object တစ်ခုကိုတိတ်တဆိတ် mutate လုပ်ခြင်းက source of truth မသိတော့စေပါတယ်။
  • Search input တစ်ခုကို ngModel ဖြင့် bind လုပ်ပြီး trimmed query, clear action နဲ့ displayed model value တို့ကိုစမ်းပါ။ ထို့နောက် input/output pair နဲ့ custom control အယူအဆပြန်ရေးပါ။

Angular — Templates — Angular

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

  • Parent နဲ့ child နှစ်ဖက်လုံးက object တစ်ခုကိုတိတ်တဆိတ် mutate လုပ်ခြင်းက source of truth မသိတော့စေပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Input box ထဲတွင် စာရိုက်လိုက်သည်နှင့်အမျှ အောက်ကစာသားသည် 'Hello, [typed text]!' ဟုချက်ချင်းပြောင်းလဲသွားမည်)

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

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

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

Two-Way Binding [( )] | Thuta Learning