Created
June 13, 2017 07:42
-
-
Save dinusuresh/e6a6585de8ce0b300b5a315369681461 to your computer and use it in GitHub Desktop.
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 {AfterViewInit, Directive, ElementRef, OnInit, Optional, Renderer} from '@angular/core'; | |
import {ModalDirective} from 'ng2-bootstrap'; | |
@Directive({ | |
selector: '[appAutoFocus]' | |
}) | |
export class AutoFocusDirective implements OnInit, AfterViewInit { | |
constructor(private el: ElementRef, private renderer: Renderer, @Optional() private modal: ModalDirective) { | |
} | |
ngOnInit() { | |
} | |
ngAfterViewInit() { | |
if (!this.modal) { | |
this.renderer.invokeElementMethod(this.el.nativeElement, 'focus', []); | |
} else { | |
this.modal.onShown.subscribe( | |
() => { | |
this.renderer.invokeElementMethod(this.el.nativeElement, 'focus', []); | |
} | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment