Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Last active August 29, 2015 14:01
Show Gist options
  • Save alexanderjeurissen/4757fc711e48dfd9cfe6 to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/4757fc711e48dfd9cfe6 to your computer and use it in GitHub Desktop.
This gist is a slightly modified version from the plunker of Emil van Galen. It basically forces angular to set a ng-model value to null instead of an empty string (when this directive is applied and the model value is empty). a blog post about the issue and some additional solutions can be found here: (http://blog.jdriven.com/2013/09/how-angula…
'use strict';
angular.module('ModuleName')
.directive('fixParsingInconsistencies', function () {
return {
require: '?ngModel',
priority: (angular.version.full.indexOf('1.2.') === 0 &&
angular.version.full !== '1.2.0rc1' &&
angular.version.full !== '1.2.0-rc.2') ? 1 : 0,
restrict: 'A',
link: function (scope, element, attrs, ngModelController) {
ngModelController.$parsers.push(function (value) {
if ((ngModelController.$invalid && angular.isUndefined(value)) || value === '') {
return null;
} else {
return value;
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment