Skip to content

Instantly share code, notes, and snippets.

@ackuser
Forked from TigorC/hover-class.directive.ts
Created April 23, 2018 12:12
Show Gist options
  • Select an option

  • Save ackuser/615c17a29e5b645a77ec7297f6677d2e to your computer and use it in GitHub Desktop.

Select an option

Save ackuser/615c17a29e5b645a77ec7297f6677d2e to your computer and use it in GitHub Desktop.
Angular 2 hover class directive
import { Directive, Input, HostListener, Renderer, ElementRef } from '@angular/core';
@Directive({ selector: '[hoverClass]' })
export class HoverClassDirective {
@Input()
hoverClass: string;
constructor(
public elementRef: ElementRef,
private renderer: Renderer
) { }
@HostListener('mouseover') mouseover() {
this.renderer.setElementClass(this.elementRef.nativeElement, this.hoverClass, true);
}
@HostListener('mouseout') mouseout() {
this.renderer.setElementClass(this.elementRef.nativeElement, this.hoverClass, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment