Skip to content

Instantly share code, notes, and snippets.

@jarontai
Created November 13, 2013 02:35
Show Gist options
  • Save jarontai/7442668 to your computer and use it in GitHub Desktop.
Save jarontai/7442668 to your computer and use it in GitHub Desktop.
make angular display invalid input value
app.directive('input', function() {
return {
require: '?ngModel',
restrict: 'E',
link: function($scope, $element, $attrs, ngModelController) {
var inputType = angular.lowercase($attrs.type);
if (!ngModelController || inputType === 'radio' ||
inputType === 'checkbox') {
return;
}
ngModelController.$formatters.unshift(function(value) {
if (ngModelController.$invalid && angular.isUndefined(value)
&& typeof ngModelController.$modelValue === 'string') {
return ngModelController.$modelValue;
} else {
return value;
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment