.focus() method on FormControl or access to native HTML element
See original GitHub issueI’m submitting a … (check one with “x”)
[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior There seems to be no way to focus a FormControl.
Expected behavior FormControl provides a .focus() instance method, or access to the native HTML element.
What is the motivation / use case for changing the behavior? To give input focus to a control when it is invalid, as in the following example:
if (!this.form.valid) {
let invalid = <FormControl[]>Object.keys(this.form.controls).map(key => this.form.controls[key]).filter(ctl => ctl.invalid);
if (invalid.length > 0) {
// Give input focus to invalid[0]... how?
}
return false;
}
Please tell us about your environment:
-
Angular version: 2.2.1
-
Browser: N/A
-
Language: TypeScript 2.0.3
Issue Analytics
- State:
- Created 7 years ago
- Reactions:15
- Comments:15 (2 by maintainers)
Top Results From Across the Web
How do I programmatically set focus to dynamically created ...
What you'd need to do is have an element reference to them, somehow, and call .focus() on that. You can achieve this through...
Read more >How to Set Focus on Form Control in Angular - onthecode
Inject Renderer 2 in the constructor and access the input element with selectRootElement() method. Then, we can focus on the input using focus...
Read more >Angular FormControl focus and blur Event - ConcretePage.com
On this page we will provide focus and blur events example with Angular FormControl. The focus and blur events are opposite to each...
Read more >Focus Using Formcontrolname As Selector - StackBlitz
Starter project for Angular apps that exports to the Angular CLI.
Read more >how to access Native HTML Input element using formControl ...
You could give your notifications component the name of the control and the form and look it up, as you are doing, but...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

As long, as Angular doesn’t provide this functionality, i manually added it:
Add the following code over your component:
Now you can just implement it your way:
In my case, it worked out of the box. However, Typescript complained because it doesn’t know nativeElement. So i did it the following way:
I hope that I could help someone 😉
Hi @kara I see #12463 was closed because “a relatively straightforward API already exists”.
Please can you show how I could give focus to an invalid form control? Thanks.