Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.