When the value of the ng-model is "24" (string) instead of 24 (number), on an input[type="number"] it throws following error:
Error: [ngModel:numfmt] http://errors.angularjs.org/1.5.8/ngModel/numfmt?p0=24
I tried to set the input as follow:
<input class="form-control" type="number" name="HouseNumber"
ng-model="Number(HouseNumber)" />
However, this throws also an error:
Error: [ngModel:nonassign] http://errors.angularjs.org/1.5.8/ngModel/nonassign
How can I let AngularJS ignore this error / strict binding, or convert this automaticly to a number in the ng-model?
I cannot set the input to type="text".
Just to visualize the error
angular.module("app", [])
.controller("controller", function ($scope) {
// I cannot simply change this in the actual code to $scope.number = 24;
$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>
When changing the input type to type="text" it works
I can't do this however in the actual code, as the input should stay a number.
angular.module("app", [])
.controller("controller", function ($scope) {
// I cannot simply change this in the actual code to $scope.number = 24;
$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>
ng-model="HouseNumber" ng-change="Number(HouseNumber)"and in your controller:$scope.Number = (x) => {return Number(x);}ng-modelon a function