question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.focus() method on FormControl or access to native HTML element

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:15
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
JunusErgincommented, Nov 21, 2017

As long, as Angular doesn’t provide this functionality, i manually added it:

Add the following code over your component:

const originFormControlNgOnChanges = FormControlDirective.prototype.ngOnChanges;
FormControlDirective.prototype.ngOnChanges = function () {
    this.form.nativeElement = this.valueAccessor._elementRef.nativeElement;
    return originFormControlNgOnChanges.apply(this, arguments);
};

const originFormControlNameNgOnChanges = FormControlName.prototype.ngOnChanges;
FormControlName.prototype.ngOnChanges = function () {
    const result = originFormControlNameNgOnChanges.apply(this, arguments);
    this.control.nativeElement = this.valueAccessor._elementRef.nativeElement;
    return result;
};

Now you can just implement it your way:

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) {
     invalid[0].nativeElement.focus();
  }
  return false;
}

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:

        if (invalid.length > 0) {
            let invalidElem: any = invalid[0];
            invalidElem.nativeElement.focus();
        }

I hope that I could help someone 😉

14reactions
pcroccommented, Feb 16, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found