Skip to content

Instantly share code, notes, and snippets.

@rayonhunte
Created June 22, 2018 13:02
Show Gist options
  • Save rayonhunte/ead055c662c705d8a7fd7d8d201a73e8 to your computer and use it in GitHub Desktop.
Save rayonhunte/ead055c662c705d8a7fd7d8d201a73e8 to your computer and use it in GitHub Desktop.
import { Directive, Renderer2, ElementRef, HostListener, HostBinding, Input } from '@angular/core';
import { MockNgModuleResolver } from '@angular/compiler/testing';
@Directive({
selector: '[appBetterHighlight]'
})
export class BetterHighlightDirective {
@Input() defaultColor = 'transparent';
@Input() highlightColor = 'blue';
@HostBinding('style.backgroundColor') background: string;;
constructor(private elRef: ElementRef, private renderer: Renderer2) { }
ngOnInit(): void {
this.background = this.defaultColor;
this.renderer.setStyle(this.elRef.nativeElement, 'background-color', 'blue');
}
@HostListener('mouseenter') mouseover(eventData: Event) {
// removed for host binding example
// this.renderer.setStyle(this.elRef.nativeElement, 'background-color', 'pink');
this.background = this.highlightColor;
}
@HostListener('mouseleave') mouseleave(eventData: Event) {
// this.renderer.setStyle(this.elRef.nativeElement, 'background-color', 'transparent');
this.background = this.defaultColor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment