AngularJS browser autofill workaround by using a directive

Apparently this is a known issue with Angular and is currently open

I’m not sure what you could do here besides some sort of work around like you’re trying. It seems you’re on the right track. I couldn’t get my browser to try to remember a password for your plunk, so I’m not sure if this will work but have a look:

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>

I think you just need to simplify your approach a bit. The one thing I definitely recommend is to check ngModel.$pristine and make sure you’re not overwriting some poor user’s input. Also, 3 seconds is probably too long. You shouldn’t have to call $apply() in a $timeout, BTW, it should queue a $digest for you automatically.

The real catch: Will your browser beat Angular to execution? What about my browser?

This is probably an unwinnable war, which is why Angular (or Knockout) hasn’t been able to solve it readily. There’s no guarantee of the state of the data in your input at the time of the directive’s initial execution. Not even at the time of Angular’s initialization…. So it’s a tricky problem to solve.

Leave a Comment