Skip to content

Instantly share code, notes, and snippets.

@toboqus
Created September 20, 2014 12:07
Show Gist options
  • Save toboqus/f39d436505192f740119 to your computer and use it in GitHub Desktop.
Save toboqus/f39d436505192f740119 to your computer and use it in GitHub Desktop.
myApp.directive('capitalizeFirst', function($parse) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if (inputValue === undefined) { inputValue = ''; }
var capitalized = inputValue.charAt(0).toUpperCase() +
inputValue.substring(1);
if(capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
return capitalized;
}
modelCtrl.$parsers.push(capitalize);
capitalize($parse(attrs.ngModel)(scope)); // capitalize initial value
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment