Last active
March 14, 2017 18:01
-
-
Save joergpatz/12fefffa56db30ae2535ba33e08af7db to your computer and use it in GitHub Desktop.
ng2-focus-forward-directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, HostListener, ElementRef, Renderer } from '@angular/core'; | |
@Directive({ | |
selector: 'input' | |
}) | |
export class InputFocusForwarderDirective { | |
constructor(private elRef: ElementRef, private renderer: Renderer) { | |
} | |
@HostListener('focus') onFocus() { | |
this.renderer.invokeElementMethod(this.elRef.nativeElement, | |
'dispatchEvent', | |
[new CustomEvent('input-focus', {bubbles: true})]); | |
// or just | |
// el.dispatchEvent(new CustomEvent('input-focus', { bubbles: true })); | |
// if you don't care about webworker compatibility | |
} | |
@HostListener('focusout') onFocusOut() { | |
this.renderer.invokeElementMethod(this.elRef.nativeElement, | |
'dispatchEvent', | |
[new CustomEvent('input-focus-out', {bubbles: true})]); | |
// or just | |
// el.dispatchEvent(new CustomEvent('input-focus', { bubbles: true })); | |
// if you don't care about webworker compatibility | |
} | |
} |
Author
joergpatz
commented
Mar 14, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment