Skip to content

Instantly share code, notes, and snippets.

@dinusuresh
Created June 13, 2017 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinusuresh/e6a6585de8ce0b300b5a315369681461 to your computer and use it in GitHub Desktop.
Save dinusuresh/e6a6585de8ce0b300b5a315369681461 to your computer and use it in GitHub Desktop.
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