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

Custom Directives

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

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

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

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

Custom Directives ကို ရိုးရိုးလေးနားလည်မယ်

Custom Directive များသည် DOM elements များ၏ behavior ကို modify လုပ်ရန် ဖန်တီးနိုင်သည်။

Problem → Why → Solution

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

`document`/nativeElement ကိုမကာကွယ်ဘဲတိုက်ရိုက်သုံးခြင်းက SSR, testing နဲ့ security boundary များပျက်စေနိုင်ပါတယ်။

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

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

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

Attribute directive က existing element/component ၏ behavior ကို reusable ပုံစံနဲ့တိုးချဲ့ပါတယ်။ Input configuration, host bindings/listeners နဲ့ injected dependencies ကိုသုံးနိုင်သော်လည်း DOM ကိုတိုက်ရိုက်ပြောင်းတာထက် declarative host APIs ကိုဦးစားပေးပါ။ Directive တစ်ခုက responsibility သေးသေးတစ်ခုသာပိုင်ပြီး keyboard, cleanup နဲ့ server rendering compatibility ကိုစစ်ရပါတယ်။

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

Custom Directives ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// highlight.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  @Input() highlightColor = 'yellow';
  
  constructor(private el: ElementRef) { }
  
  @HostListener('mouseenter') onMouseEnter() {
    this.highlight(this.highlightColor);
  }
  
  @HostListener('mouseleave') onMouseLeave() {
    this.highlight('');
  }
  
  private highlight(color: string) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

// Usage in template
<p appHighlight highlightColor="lightblue">
  Hover over me!
</p>

Attribute directive က existing element/component ၏ behavior ကို reusable ပုံစံနဲ့တိုးချဲ့ပါတယ်။ Input configuration, host bindings/listeners နဲ့ injected dependencies ကိုသုံးနိုင်သော်လည်း DOM ကိုတိုက်ရိုက်ပြောင်းတာထက် declarative host APIs ကိုဦးစားပေးပါ။ Directive တစ်ခုက responsibility သေးသေးတစ်ခုသာပိုင်ပြီး keyboard, cleanup နဲ့ server rendering compatibility ကိုစစ်ရပါတယ်။

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

  1. Input + state// highlight.directive.ts

    Custom Directive များသည် DOM elements များ၏ behavior ကို modify လုပ်ရန် ဖန်တီးနိုင်သည်။

  2. Component + template@HostListener('mouseleave') onMouseLeave() {

    Attribute directive က existing element/component ၏ behavior ကို reusable ပုံစံနဲ့တိုးချဲ့ပါတယ်။ Input configuration, host bindings/listeners နဲ့ injected dependencies ကိုသုံးနိုင်သော်လည်း DOM ကိုတိုက်ရိုက်ပြောင်းတာထက် declarative host APIs ကိုဦးစားပေးပါ။ Directive တစ်ခုက responsibility သေးသေးတစ်ခုသာပိုင်ပြီး keyboard, cleanup နဲ့ server rendering compatibility ကိုစစ်ရပါတယ်။

  3. Rendered Angular UI</p>

    Auto-focus သို့ highlight directive တစ်ခုရေးပြီး input ပြောင်းမှု, disabled state, keyboard event နဲ့ destroy cleanup ကိုစမ်းပါ။

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

Success

Custom Directives ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

`document`/nativeElement ကိုမကာကွယ်ဘဲတိုက်ရိုက်သုံးခြင်းက SSR, testing နဲ့ security boundary များပျက်စေနိုင်ပါတယ်။

Custom Directive များသည် DOM elements များ၏ behavior ကို modify လုပ်ရန် ဖန်တီးနိုင်သည်။

typescript
// highlight.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  @Input() highlightColor = 'yellow';
  
  constructor(private el: ElementRef) { }
  
  @HostListener('mouseenter') onMouseEnter() {
    this.highlight(this.highlightColor);
  }
  
  @HostListener('mouseleave') onMouseLeave() {
    this.highlight('');
  }
  
  private highlight(color: string) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

// Usage in template
<p appHighlight highlightColor="lightblue">
  Hover over me!
</p>
You should see
(Mouse ဖြင့် paragraph အပေါ်ရောက်လျှင် lightblue background ပေါ်လာမည်)

Real-world usage

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

Auto-focus သို့ highlight directive တစ်ခုရေးပြီး input ပြောင်းမှု, disabled state, keyboard event နဲ့ destroy cleanup ကိုစမ်းပါ။

Mini challenge

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

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

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

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  @Input() highlightColor = 'yellow';
  
  constructor(private el: ElementRef) { }
  
  @HostListener('mouseenter') onMouseEnter() {
    this.highlight(this.highlightColor);
  }
  
  @HostListener('mouseleave') onMouseLeave() {
    this.highlight('');
  }
  
  private highlight(color: string) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

// Usage in template
<p appHighlight highlightColor="lightblue">
  Hover over me!
</p>

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

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

Auto-focus သို့ highlight directive တစ်ခုရေးပြီး input ပြောင်းမှု, disabled state, keyboard event နဲ့ destroy cleanup ကိုစမ်းပါ။

Quick Quiz 1 / 3

Custom Directives ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Input + state → Component + template → Rendered Angular UI
  • Attribute directive က existing element/component ၏ behavior ကို reusable ပုံစံနဲ့တိုးချဲ့ပါတယ်။ Input configuration, host bindings/listeners နဲ့ injected dependencies ကိုသုံးနိုင်သော်လည်း DOM ကိုတိုက်ရိုက်ပြောင်းတာထက် declarative host APIs ကိုဦးစားပေးပါ။ Directive တစ်ခုက responsibility သေးသေးတစ်ခုသာပိုင်ပြီး keyboard, cleanup နဲ့ server rendering compatibility ကိုစစ်ရပါတယ်။
  • `document`/nativeElement ကိုမကာကွယ်ဘဲတိုက်ရိုက်သုံးခြင်းက SSR, testing နဲ့ security boundary များပျက်စေနိုင်ပါတယ်။
  • Auto-focus သို့ highlight directive တစ်ခုရေးပြီး input ပြောင်းမှု, disabled state, keyboard event နဲ့ destroy cleanup ကိုစမ်းပါ။

Angular — Directives — Angular

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

  • `document`/nativeElement ကိုမကာကွယ်ဘဲတိုက်ရိုက်သုံးခြင်းက SSR, testing နဲ့ security boundary များပျက်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Mouse ဖြင့် paragraph အပေါ်ရောက်လျှင် lightblue background ပေါ်လာမည်)

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

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

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

Custom Directives | Thuta Learning