Created
July 3, 2015 00:36
-
-
Save lelandcope/50cb1f98d02472f5c0dc to your computer and use it in GitHub Desktop.
AngularJS Lowercase Directive that will force the input to be lowercased
This file contains 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
_module.directive('lowercase', function () { | |
return { | |
require: 'ngModel', | |
link: function ($scope, elem, attrs, ngModelCtrl) { | |
var lowercase = function (inputValue) { | |
if(inputValue == undefined) inputValue = ''; | |
var lowercased = inputValue.toLowerCase(); | |
if(lowercased !== inputValue) { | |
ngModelCtrl.$setViewValue(lowercased); | |
ngModelCtrl.$render(); | |
} | |
return lowercased; | |
} | |
ngModelCtrl.$parsers.unshift(lowercase); | |
lowercase($scope[attrs.ngModel]); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment