Test
HTML
<div ng-app="myApp">
<div ng-controller="MyCont">
<input type="number" min="-10" max="50" value="{{value}}" ng-model="value"/>
<input type="range" min="-10" max="50" value="{{value}}" ng-model="value" numericsaving/>
<input type="text" value="{{value}}" ng-model="value" numericsaving/>
<br>{{value}}:{{ typeOfValue() }}
</div>
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('MyCont', function($scope) {
$scope.value = 10;
$scope.typeOfValue=function(){
return (typeof $scope.value ==='string')?'string':'number'
};
});
myApp.directive('numericsaving', function () {
return {
restrict: 'A',
require: '?ngModel',
scope: {
model: '=ngModel'
},
link: function (scope, element, attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
ngModelCtrl.$parsers.push(function (value) {
if (!value || value==='' || isNaN(parseInt(value)) || parseInt(value)!=value) {
value=0;
}
return parseInt(value);
});
}
};
});