Skip to content

Instantly share code, notes, and snippets.

@Werninator
Last active May 31, 2016 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Werninator/6263d126162bcb6dda95 to your computer and use it in GitHub Desktop.
Save Werninator/6263d126162bcb6dda95 to your computer and use it in GitHub Desktop.
ngrChange - ng REAL change - only trigger change on value change at pressing enter or blur out.
app.directive('ngrChange', function() {
return {
require: 'ngModel',
link: function (scope, element, attrs) {
var valueBefore = element.val();
element.bind('focus', function() {
if (valueBefore !== element.val())
valueBefore = element.val();
});
element.bind('keydown keypress', function(e) {
if (e.which !== 13
|| element.val() === valueBefore)
return;
valueBefore = element.val();
scope.$apply(function() {
scope.$eval(attrs.ngrChange);
});
});
element.bind('blur', function(e) {
if (element.val() === valueBefore)
return;
valueBefore = element.val();
scope.$apply(function() {
scope.$eval(attrs.ngrChange);
});
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment