Skip to content

Instantly share code, notes, and snippets.

@J2D2Development
Last active September 1, 2017 12:57
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 J2D2Development/16e7b668682d2b0b0c976ff1cdbcf64d to your computer and use it in GitHub Desktop.
Save J2D2Development/16e7b668682d2b0b0c976ff1cdbcf64d to your computer and use it in GitHub Desktop.
import { AfterViewInit, Directive, ElementRef } from '@angular/core';
@Directive({ selector: '[ifautofocus]' })
export class IfAutofocus implements AfterViewInit {
constructor(private elementRef: ElementRef) {}
ngAfterViewInit() {
this.elementRef.nativeElement.focus();
}
}
/*
remember to include it in your module's 'declarations' array,
then import it into your component.
add it like you would the 'autofocus' tag in your template:
<input formControlName="searchTerm" type="text" placeholder="Search!" ifautofocus />
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment