Created
September 17, 2016 09:36
-
-
Save navix/e8a51a9240031281d66d5c28d54984d2 to your computer and use it in GitHub Desktop.
Angular 2 // Element autofocus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works well. This works for me, avoiding the need for
renderer
:this.input.nativeElement.focus()