Last active
December 6, 2024 18:44
-
-
Save tuanngominh/d2b60ca378e6bda8c6e943465535e4cc to your computer and use it in GitHub Desktop.
input focus in angular using nativeElement
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, 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good example