Last active
December 31, 2015 06:19
-
-
Save nelsonpecora/7947233 to your computer and use it in GitHub Desktop.
Hacky directive that "fixes" autofill
This file contains 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
// autofill catcher (super hacky, abandon all hope ye who enter here --np) | |
app.directive('autofill', ['$timeout', function($timeout) { | |
return { | |
restrict: 'A', | |
require: 'ngModel', | |
link: function( scope, elem, attrs ) { | |
var ownInput = false; | |
// trigger an input 500ms after loading the page (fixes chrome and safari autofill) | |
$timeout(function() { | |
$(elem[0]).trigger('input'); | |
}, 500); | |
// listen for pertinent events to trigger input on form element | |
// use timeout to ensure val is populated before triggering 'input' | |
// ** 'change' event fires for Chrome | |
// ** 'DOMAttrModified' fires for Firefox 22.0 | |
// ** 'keydown' for Safari 6.0.1 | |
// ** 'propertychange' for IE | |
elem.on('change DOMAttrModified keydown propertychange', function() { | |
$timeout(function() { | |
$(elem[0]).trigger('input'); | |
}, 0); | |
}); | |
// tell other inputs to trigger (fixes firefox and ie9+ autofill) | |
elem.on('input', function() { | |
if(ownInput === false) scope.$emit('loginform.input'); | |
}); | |
// catches event and triggers if another input fired it. | |
scope.$on('loginform.input', function(e) { | |
e.stopPropagation(); | |
ownInput = true; | |
$(elem[0]).trigger('input'); | |
ownInput = false; | |
}); | |
} | |
} | |
}]); |
Because this directive relies on jQuery to trigger events.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you know why jslint is saying '$' isn't defined?