Directiveها در انگولار به منزله یک فراخوانی یک سری از دستورها ، شرط و عملیات یا کدها برای یک تگ html ای میباشند یعنی اگر درون یک تگ از یک دایرکتیو استفاده شود یک سری خصوصیات به اون تگ اضافه میشه .
import { Directive , HostListener ,ElementRef} from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(private element : ElementRef) {
this.highlight(null);
}
@HostListener('mouseenter') public onMouseEnterCustom(){
this.highlight("Gray");
}
@HostListener('mouseleave') public onMouseExitCustom(){
this.highlight(null);
}
public highlight(color : string){
this.element.nativeElement.style.backgroundColor = color;
}
}<p appHighlight>
Directive and EventListener Example!
</p>
لطفا منتظر بمانید...