Skip to content

Instantly share code, notes, and snippets.

@djvs
Created January 31, 2016 13:05
Show Gist options
  • Save djvs/bf393cf5d53ec1e27b54 to your computer and use it in GitHub Desktop.
Save djvs/bf393cf5d53ec1e27b54 to your computer and use it in GitHub Desktop.
Angular force number with range on input[type='text']
app.directive('forcenum', function ($parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function link(scope, element, attr, ngModel) {
var max = parseInt(attr.fmax);
var min = parseInt(attr.fmin);
var nv;
scope.$watch(function () {
return ngModel.$modelValue;
}, function (val) {
var value = parseInt(val);
if (value > max) {
nv = max;
} else if (value < min) {
nv = min;
} else if (!value) {
return;
} else {
nv = value;
}
log(value, nv);
ngModel.$setViewValue(nv);
element.val(nv);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment