Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

input type=“number” not clearable for non-number (NaN) #16654

Closed
@georgeawg

Description

@georgeawg

I'm submitting a ...

  • [x ] bug report

Current behavior:

The ng-model directive is unable to clear the content of an <input type="number"> element when that content parses to NaN (not a number). This can happen when a user pastes invalid content or simply types "eee".

Expected / new behavior:

Setting the ng-model input to null should clear the input.

Minimal reproduction of the problem with instructions:

See jsFiddle - enter "eee", click the "X"

AngularJS version: 1.7

Browser: [all | Chrome XX | Firefox XX | Edge XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

Chrome

Anything else:

One fix is to add a custom directive:

app.directive('clearNan', function() {
  return {
    require: 'ngModel',
    link: function(scope, elem, attrs, ngModel) {
      ngModel.$formatters.push(function(value) {
        if (!value) elem.val(null);
        return value;
      });
    }
  };
})

See Stack Overflow: AngularJS - input type=“number” not clearable for non-number (NaN)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions