Skip to content

Instantly share code, notes, and snippets.

@EpokK
Created June 14, 2014 12:34
Show Gist options
  • Save EpokK/50db48dbcbcae4a2d62d to your computer and use it in GitHub Desktop.
Save EpokK/50db48dbcbcae4a2d62d to your computer and use it in GitHub Desktop.
An AngulaJS directive to force uppercase in input
angular.module('myApp')
.directive('capitalize', function () {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if(angular.isDefined(inputValue)) {
var capitalized = inputValue.toUpperCase();
if(capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
return capitalized;
}
}
modelCtrl.$parsers.push(capitalize);
capitalize(scope[attrs.ngModel]); // capitalize initial value
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment