Last active
August 28, 2017 10:59
-
-
Save tuanngominh/0c679d5f3ad51ac79d3c2655dfdb4fd2 to your computer and use it in GitHub Desktop.
input focus in angular through renderer
This file contains hidden or 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
import { Component, AfterContentInit, Renderer2 } from '@angular/core'; | |
@Component({ | |
selector: 'custom-form', | |
template: ` | |
<form> | |
<input | |
placeholder="Name" | |
id="name-input" | |
/> | |
</form> | |
` | |
}) | |
export class FormComponent implements AfterContentInit { | |
constructor(private renderer: Renderer2) {} | |
// Change state before angular change detection complete | |
ngAfterContentInit() { | |
this.renderer.selectRootElement('#name-input').focus(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment