What happens when the value of ng-model
is a "24" (string)
instead of 24 (number)
on an input[type="number"]
? It triggers the following error:
Error: [ngModel:numfmt] http://errors.angularjs.org/1.5.8/ngModel/numfmt?p0=24
I attempted to set the input like this:
<input class="form-control" type="number" name="HouseNumber"
ng-model="Number(HouseNumber)" />
However, this also resulted in an error:
Error: [ngModel:nonassign] http://errors.angularjs.org/1.5.8/ngModel/nonassign
Is there a way to make AngularJS
ignore this error/strict binding, or automatically convert it to a number
in the ng-model
?
I'm unable to change the input to type="text"
.
To better understand the issue
angular.module("app", [])
.controller("controller", function ($scope) {
// Changing this to $scope.number = 24; directly in the code is not possible
$scope.number = "24";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<input type="number" ng-model="number"/>
</div>
Switching the input type to type="text"
resolves the issue
However, this is not feasible in the actual code, as the input must remain a number.
angular.module("app", [])
.controller("controller", function ($scope) {
// Changing this to $scope.number = 24; directly in the code is not possible
$scope.number = "24";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<input type="text" ng-model="number"/>
</div>