focus on input angular js code example

Example 1: focuses on the input field automatically on page load angular

$ ng g directive autofocus

Example 2: focuses on the input field automatically on page load angular

import { Directive, AfterViewInit, ElementRef } from '@angular/core';
 
@Directive({
  selector: '[appAutofocus]'
})
export class AutofocusDirective implements AfterViewInit {
 
  constructor(private el: ElementRef) {
  }
 
  ngAfterViewInit() {
    this.el.nativeElement.focus();
  }
 
}