Skip to content

Instantly share code, notes, and snippets.

@navix
Created September 17, 2016 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save navix/e8a51a9240031281d66d5c28d54984d2 to your computer and use it in GitHub Desktop.
Save navix/e8a51a9240031281d66d5c28d54984d2 to your computer and use it in GitHub Desktop.
Angular 2 // Element autofocus
@Component({
selector: 'my-comp',
template: `
<input #myInput type="text" />
<div> Some other content </div>
`
})
export class MyComp implements AfterViewInit {
@ViewChild('myInput') input: ElementRef;
constructor(private renderer: Renderer) {}
ngAfterViewInit() {
this.renderer.invokeElementMethod(this.input.nativeElement, 'focus');
}
}
@whyboris
Copy link

Works well. This works for me, avoiding the need for renderer:
this.input.nativeElement.focus()

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