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

Attribute: ngStyle

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

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

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

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

Attribute: ngStyle ကို ရိုးရိုးလေးနားလည်မယ်

ngStyle directive သည် element တစ်ခုတွင် inline styles များကို dynamically set လုပ်ရန် အသုံးပြုသည်။

Problem → Why → Solution

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

User input ကို style object ထဲတိုက်ရိုက်ထည့်ခြင်းနဲ့ bounds မစစ်ခြင်းက broken layout သို့ unsafe values ဖြစ်စေနိုင်ပါတယ်။

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

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

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

Style binding က runtime numeric/color/layout value တချို့အတွက်အသုံးဝင်သော်လည်း reusable visual variants အများစုကို CSS classes ဖြင့်ထားသင့်ပါတယ်။ Unit suffix binding က number နဲ့ CSS unit contract ကိုရှင်းစေပြီး style sanitization ရှိသော်လည်း arbitrary user-controlled CSS ကိုမယုံသင့်ပါ။ Accessibility အတွက် color တစ်ခုတည်းနဲ့ meaning မဖော်ပြပါနှင့်။

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

Attribute: ngStyle ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
<div [ngStyle]="{
  'background-color': bgColor,
  'font-size': fontSize + 'px',
  'font-weight': isBold ? 'bold' : 'normal',
  'padding': '20px'
}">
  Styled Content
</div>

// component.ts
export class MyComponent {
  bgColor = '#3498db';
  fontSize = 18;
  isBold = true;
}

Style binding က runtime numeric/color/layout value တချို့အတွက်အသုံးဝင်သော်လည်း reusable visual variants အများစုကို CSS classes ဖြင့်ထားသင့်ပါတယ်။ Unit suffix binding က number နဲ့ CSS unit contract ကိုရှင်းစေပြီး style sanitization ရှိသော်လည်း arbitrary user-controlled CSS ကိုမယုံသင့်ပါ။ Accessibility အတွက် color တစ်ခုတည်းနဲ့ meaning မဖော်ပြပါနှင့်။

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

  1. Input + state<div [ngStyle]="{

    ngStyle directive သည် element တစ်ခုတွင် inline styles များကို dynamically set လုပ်ရန် အသုံးပြုသည်။

  2. Component + template</div>

    Style binding က runtime numeric/color/layout value တချို့အတွက်အသုံးဝင်သော်လည်း reusable visual variants အများစုကို CSS classes ဖြင့်ထားသင့်ပါတယ်။ Unit suffix binding က number နဲ့ CSS unit contract ကိုရှင်းစေပြီး style sanitization ရှိသော်လည်း arbitrary user-controlled CSS ကိုမယုံသင့်ပါ။ Accessibility အတွက် color တစ်ခုတည်းနဲ့ meaning မဖော်ပြပါနှင့်။

  3. Rendered Angular UI}

    Progress bar width ကို validated 0–100 value နဲ့ bind လုပ်ပြီး class-based status label နဲ့ screen-reader text ထည့်ပါ။

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

Success

Attribute: ngStyle ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

User input ကို style object ထဲတိုက်ရိုက်ထည့်ခြင်းနဲ့ bounds မစစ်ခြင်းက broken layout သို့ unsafe values ဖြစ်စေနိုင်ပါတယ်။

ngStyle directive သည် element တစ်ခုတွင် inline styles များကို dynamically set လုပ်ရန် အသုံးပြုသည်။

typescript
<div [ngStyle]="{
  'background-color': bgColor,
  'font-size': fontSize + 'px',
  'font-weight': isBold ? 'bold' : 'normal',
  'padding': '20px'
}">
  Styled Content
</div>

// component.ts
export class MyComponent {
  bgColor = '#3498db';
  fontSize = 18;
  isBold = true;
}
You should see
(Div သည် blue background, 18px font size, နှင့် bold font weight ဖြင့် ပေါ်လာမည်)

Real-world usage

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

Progress bar width ကို validated 0–100 value နဲ့ bind လုပ်ပြီး class-based status label နဲ့ screen-reader text ထည့်ပါ။

Mini challenge

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

Attribute: ngStyle နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
<div [ngStyle]="{
  'background-color': bgColor,
  'font-size': fontSize + 'px',
  'font-weight': isBold ? 'bold' : 'normal',
  'padding': '20px'
}">
  Styled Content
</div>

// component.ts
export class MyComponent {
  bgColor = '#3498db';
  fontSize = 18;
  isBold = true;
}

ရလဒ်: Attribute: ngStyle ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

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

Progress bar width ကို validated 0–100 value နဲ့ bind လုပ်ပြီး class-based status label နဲ့ screen-reader text ထည့်ပါ။

Quick Quiz 1 / 3

Attribute: ngStyle ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Input + state → Component + template → Rendered Angular UI
  • Style binding က runtime numeric/color/layout value တချို့အတွက်အသုံးဝင်သော်လည်း reusable visual variants အများစုကို CSS classes ဖြင့်ထားသင့်ပါတယ်။ Unit suffix binding က number နဲ့ CSS unit contract ကိုရှင်းစေပြီး style sanitization ရှိသော်လည်း arbitrary user-controlled CSS ကိုမယုံသင့်ပါ။ Accessibility အတွက် color တစ်ခုတည်းနဲ့ meaning မဖော်ပြပါနှင့်။
  • User input ကို style object ထဲတိုက်ရိုက်ထည့်ခြင်းနဲ့ bounds မစစ်ခြင်းက broken layout သို့ unsafe values ဖြစ်စေနိုင်ပါတယ်။
  • Progress bar width ကို validated 0–100 value နဲ့ bind လုပ်ပြီး class-based status label နဲ့ screen-reader text ထည့်ပါ။

Angular — Templates — Angular

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

  • User input ကို style object ထဲတိုက်ရိုက်ထည့်ခြင်းနဲ့ bounds မစစ်ခြင်းက broken layout သို့ unsafe values ဖြစ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

Attribute: ngStyle နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Div သည် blue background, 18px font size, နှင့် bold font weight ဖြင့် ပေါ်လာမည်)

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

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

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

Attribute: ngStyle | Thuta Learning