When submitting a form in AngularJS and use the browser remember password functionality, and in a subsequent login attempt you let the browser fill in the login form with the username and password, the $scope
model won't be changed based on the autofill.
The only dirty hack I found is to use the following directive:
app.directive("xsInputSync", ["$timeout" , function($timeout) {
return {
restrict : "A",
require: "?ngModel",
link : function(scope, element, attrs, ngModel) {
$timeout(function() {
if (ngModel.$viewValue && ngModel.$viewValue !== element.val()) {
scope.apply(function() {
ngModel.$setViewValue(element.val());
});
}
console.log(scope);
console.log(ngModel.$name);
console.log(scope[ngModel.$name]);
}, 3000);
}
};
}]);
The problem is that the ngModel.$setViewValue(element.val());
doesn't change the model nor the view based on the element.val()
returned value. How can I accomplish that?
Dirty code, check if issue https://github.com/angular/angular.js/issues/1460#issuecomment-18572604 is fixed before using this code. This directive triggers events when field is filled, not only before submit (it's necessary if you have to handle input before submit)
If you want to keep it simple just get the value using javascript
In your angular js controller :
var username = document.getElementById('username').value;
This is jQuery way :
This is Angular way :
HTML
None of these solutions worked for my use case. I have some form fields that use ng-change to watch for change. Using
$watch
is no help as it is not triggered by autofill. Since I have no submit button there is no easy way to run some of the solutions and I was not successful using intervals.I ended up disabling autofill - not ideal but a lot less confusing to the user.
Found the answer here
Seems like clear straight ahead solution. No jQuery needed.
UPDATE:
This is the solution I ended up using in my forms.
And the form's template will be
This way I was able to run any parsers/formatters I have on my ng-model and have the submit functionality transparent.