Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tuanngominh/0c679d5f3ad51ac79d3c2655dfdb4fd2 to your computer and use it in GitHub Desktop.
Save tuanngominh/0c679d5f3ad51ac79d3c2655dfdb4fd2 to your computer and use it in GitHub Desktop.
input focus in angular through renderer
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