Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created April 6, 2016 07:37
Show Gist options
  • Select an option

  • Save hsleonis/096a5c9b0f9236f8d51c6c6d1edec28f to your computer and use it in GitHub Desktop.

Select an option

Save hsleonis/096a5c9b0f9236f8d51c6c6d1edec28f to your computer and use it in GitHub Desktop.
Filters on ng-model in an input
// <input ng-model="sth" ng-trim="false" custom-validation>
app.directive('customValidation', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
var transformedInput = inputValue.toLowerCase().replace(/ /g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment