Created
June 14, 2014 12:34
-
-
Save EpokK/50db48dbcbcae4a2d62d to your computer and use it in GitHub Desktop.
An AngulaJS directive to force uppercase in input
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
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