Last active
May 31, 2016 16:04
-
-
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.
This file contains hidden or 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
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