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

ngChange handler called, on submit, for blank type="number" fields, when they haven't changed #17181

Open
1 of 4 tasks
BigBadaboom opened this issue Feb 25, 2022 · 0 comments
Open
1 of 4 tasks

Comments

@BigBadaboom
Copy link

I suppose this doesn't meet the LTS definition of a critical bug.
But I am going to file it here in case it is - at the very least - helpful to other people.

I spent a lot of time tracking down this issue. It's confusing and non-obvious why this would happen.

I'm submitting a ...

  • regression from 1.7.0
  • security issue
  • issue caused by a new browser version
  • other

Current behavior:

If you initialise, or clear a <input type="number"> field to blank (ie ""). It's ng-change handler will always be called on submit. Even if it has not been changed.

Expected / new behavior:
Change handler does not get called.

Minimal reproduction of the problem with instructions:
An example

<div ng-app="app"> <div ng-controller="MainCtrl"> <form ng-submit="submit()"> <input type="number" ng-model="form.testNumber" ng-change="changed()"/> <button type="submit">Submit</button> </form> </div> </div>">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="MainCtrl">
    <form ng-submit="submit()">
      <input type="number" ng-model="form.testNumber" ng-change="changed()"/>
      <button type="submit">Submit</button>
    </form>
  </div>
</div>
var app = angular.module("app", []);

app.controller("MainCtrl", function ($scope) {
    $scope.form = {
      testNumber: ""
    };

    $scope.changed = function () {
        console.log("changed");
    };

    $scope.submit = function () {
        console.log("submitted");
    };
});

see: http://jsfiddle.net/6qxrygzm/1/

Click the submit button in the demo. The console will log the message "changed" even though the field is untouched from its initialised value.

AngularJS version: 1.8.2

Browser: all | Chrome 98.0.4758.102 | Firefox 97.0.1 (64-bit)

Anything else:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant