Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tuanngominh/d2b60ca378e6bda8c6e943465535e4cc to your computer and use it in GitHub Desktop.
Save tuanngominh/d2b60ca378e6bda8c6e943465535e4cc to your computer and use it in GitHub Desktop.
input focus in angular using nativeElement
import { Component, ElementRef, ViewChild, AfterContentInit } from '@angular/core';
@Component({
selector: 'custom-form',
template: `
<form>
<input
placeholder="Name"
#nameInput
/>
</form>
`
})
export class FormComponent implements AfterContentInit {
@ViewChild('nameInput') nameInput: ElementRef;
// Change state before angular change detection complete
ngAfterContentInit() {
this.nameInput.nativeElement.focus();
}
}
@fanguangjian
Copy link

good example

@Kaue2
Copy link

Kaue2 commented Dec 6, 2024

Thx, help a lot

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