Thuta Learning
ရှာဖွေရန်
IntermediateWeb Developmentintermediate

Custom Directives

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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 ပေါ်လာမည်)
Custom Directives | Thuta Learning