Last active
January 27, 2017 17:26
-
-
Save LuisEGR/5d4d88e224ff38c8278290899ac93772 to your computer and use it in GitHub Desktop.
Limit the number that user introduce in an input text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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