Skip to content

Instantly share code, notes, and snippets.

@LuisEGR
Last active January 27, 2017 17:26
Show Gist options
  • Save LuisEGR/5d4d88e224ff38c8278290899ac93772 to your computer and use it in GitHub Desktop.
Save LuisEGR/5d4d88e224ff38c8278290899ac93772 to your computer and use it in GitHub Desktop.
Limit the number that user introduce in an input text
app.directive('maxNumber', function () {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function fromUser(text) {
var maxN = parseInt(attr.maxNumber);
if (text) {
if(parseInt(text) > maxN){
ngModelCtrl.$setViewValue(maxN);
ngModelCtrl.$render();
}
return maxN;
}
return undefined;
}
ngModelCtrl.$parsers.push(fromUser);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment