Skip to content

Instantly share code, notes, and snippets.

@raviagheda
Created August 21, 2022 20:26
Show Gist options
  • Save raviagheda/ab9e5f4e501ff4aa17350fc8d353ab67 to your computer and use it in GitHub Desktop.
Save raviagheda/ab9e5f4e501ff4aa17350fc8d353ab67 to your computer and use it in GitHub Desktop.
import { Directive, ElementRef, OnInit, AfterViewInit } from '@angular/core';

@Directive({
  selector: '[appAutofocus]'
})
export class AutofocusDirective implements OnInit, AfterViewInit {


  constructor(private elementRef: ElementRef) { }

  ngOnInit(): void {
    this.elementRef.nativeElement.focus()
  }

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.elementRef.nativeElement.focus();
    }, 10);
  }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment