Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import {AfterViewInit, Directive, ElementRef, OnInit, Optional, Renderer} from '@angular/core';
import {ModalDirective} from 'ng2-bootstrap';
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.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.