I wish there was TryParse but I guess there isn't in AngularJS. I tried to use angular.isNumber(). What I want my program to do: to replace any not numeric input with number 0. What it actually does: input box ignores anything which is not numeric by not allowing to write it down.
<script>
var app = angular.module('BindingsApp', []);
app.controller('InputCtrl', function($scope) {
$scope.num3 = 0;
$scope.edit = function () {
$scope.num1 = parseInt($scope.num1);
$scope.num2 = parseInt($scope.num2);
function isNumeric(num) {
return !isNaN(num);
}
if (!angular.isNumber($scope.num1) ) {
$scope.num1 = 0;
}
if (!angular.isNumber($scope.num2) ) {
$scope.num2 = 0;
}
$scope.num3 = $scope.num1 + $scope.num2;
};
});
</script>
<input type="text" ng-model="num1" ng-change="edit()" value="isNumeric(num1)" />
<br />
<input type="text" ng-model="num2" ng-change="edit()" value="{{num2}}" />
<br />
<input type="text" ng-model="num3" ng-change="edit()" value="{{num3}}" />
<br />