Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@pcroc

Description

@pcroc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions