Skip to content

Instantly share code, notes, and snippets.

@rafakato
Created July 29, 2014 20:25
Show Gist options
  • Save rafakato/2b4e333a4e5647994114 to your computer and use it in GitHub Desktop.
Save rafakato/2b4e333a4e5647994114 to your computer and use it in GitHub Desktop.
AngularJS browser autofill workaround
app.directive('autoFillSync', function($timeout) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ngModel) {
var origVal = elem.val();
$timeout(function() {
var newVal = elem.val();
if (ngModel.$pristine && origVal !== newVal) {
ngModel.$setViewValue(newVal);
}
}, 500);
}
}
});
<form name="myForm" ng-submit="login()">
<label for="username">Username</label>
<input type="text" id="username" name="username" ng-model="username" auto-fill-sync/>
<br/>
<label for="password">Password</label>
<input type="password" id="password" name="password" ng-model="password" auto-fill-sync/>
<br/>
<button type="submit">Login</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment