Skip to content

Instantly share code, notes, and snippets.

@Maxim-Filimonov
Created July 10, 2014 02:43
Show Gist options
  • Save Maxim-Filimonov/f0d73935492e3ac0b3f9 to your computer and use it in GitHub Desktop.
Save Maxim-Filimonov/f0d73935492e3ac0b3f9 to your computer and use it in GitHub Desktop.
A Pen by Secret Sam.
<section ng-app="myApp" ng-controller="MainCtrl">
<h4 class="text-center">AngularJS Numeric Value Widget</h4>
<div class="well entry">
<label>Activity entry</label>
<input type="text" ng-model="activityUnitValue" name="unitValue" integer="" numeric="" positive="">
</div>
</section>
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
});
app.directive('numeric', function () {
return {
require: '?ngModel',
link: function (scope, element, attrs, modelCtrl) {
return modelCtrl.$parsers.push(function (inputValue) {
var transformedInput;
if (inputValue == null) {
return '';
}
transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
.entry {
width: 300px;
margin: 10px auto;
text-align: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment