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