Form model doesn't update on autocomplete
I'm having problems with Safari 6.0 auto-complete on some simple forms on Angular 1.0.2.
When Safari uses auto-complete to fill in the form, Angular seems to be
unaware of the new characters that were entered by auto-complete.
Please see the attached image for an example.
To reproduce:
- Enter data into the form a few times so that auto-complete is activated.
- Then enter a few characters that triggers an auto-complete.
- Hit return.
- Compare the alert message with the contents of the input field.
I've created a jsFiddle that can be used to reproduce the problem: http://jsfiddle.net/msgilligan/5cynT/
I've seen a similar problem when Safari 6.0 or Chrome 22.0.1229.79 automatically fills in a password field that has a 'required' attribute. The form is not marked as valid and I have to go enter a space after the password and then delete it.
Here's an example of a very (!) simple and small ng-model implementation that allows for getting the values on demand. Simply $scope.$broadcast("$myNgModelSet") and the model will be updated.
App.directive("myNgModel", ["$parse", function ($parse) {
return {
restrict: "A",
link: function ($scope, $element, $attrs) {
// Assume $element is input type="text"
var modelGet = $parse($attrs["ngModel"]);
var modelSet = modelGet.assign;
function setValueFromInputElement() {
modelSet($scope, $element.val());
}
$scope.$watch($attrs["ngModel"], function () {
$element.val(modelGet($scope));
});
$element.bind("change", function () {
setValueFromInputElement();
});
$scope.$on("$myNgModelSet", function () {
setValueFromInputElement();
})
}
}
}]);
+1
Chrome at least does indeed emit an event (https://code.google.com/p/chromium/issues/detail?id=135307), so I think there is a legitimate issue here.
In addition to a fix for the issue, it would be great to have a sanctioned workaround for browsers that do not emit an event which is explicitly forwards-compatible.
Like a timeout of some sort specifically for forms. Or at least a way to tell angular to re-check.
+1 Please fix this.
I am using a temp hack which is quite dirty:
$('#my-form input:visible, #my-form select:visible').each(function(){
$(this).trigger('input');
});
However, this will trigger another problem. I always got the following errors in concole:
Error: $apply already in progress
at Error (<anonymous>)
at beginPhase (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js:8270:15)
at Object.Scope.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js:8072:11)
at HTMLInputElement.listener (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js:11421:13)
at HTMLInputElement.jQuery.event.dispatch (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js:3058:9)
at HTMLInputElement.elemData.handle.eventHandle (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js:2676:28)
at Object.jQuery.event.trigger (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js:2941:12)
at HTMLInputElement.<anonymous> (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js:3599:17)
at Function.jQuery.extend.each (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js:611:20)
at jQuery.fn.jQuery.each (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js:241:17) angular.js:5687
whuhacker You need to call your hack outside of angular. . Try setTimeout(function() { ... }, 0) Sorry about the format, doing this with my mobile and SwiftKey.. It's not code intelligent :-)
@mike-spainhower I believe that bug report is for a different type of auto-fill than a "remember password" type auto-fill.
I've filed a new bug against Chrome here:
https://code.google.com/p/chromium/issues/detail?id=224405
Which links to a JSFiddle that shows the "Remember password" auto-fill does not trigger a change event.
@stephenh Touche! I knew that they were separate under settings but did not realize autofill and passwords had separate behavior - great catch.
Is it still fair to say that angularjs users still need a consistent and supported way to get expected behavior across browsers and versions? From what I gather the closest we have to this currently is setting autocomplete attribute to off.
@mike-spainhower I agree, that "regular autofill" and "password autofill" is different behavior was surprising to me too.
I think @whuhacker has the best hack for Chrome right now.
For Firefox, it is working for us with this change to Angular:
https://github.com/stephenh/angular.js/commit/f6295cc9b0854b00e87ef6a1012d40cb97922604
If anyone wants to use/clean up/submit that patch, please feel free.
I haven't gotten to trying anything other than Firefox/Chrome--Chrome is being so annoying about this that I'm giving up for awhile and will try again later with a fresh set of eyes.
Angularjs also does not work with LastPass auto-fill.
@drozzy How does LastPass auto-fill work? It should either a) fill in the values before body.onload runs (in which case my patch to Angular should fix it, like it did Firefox), or b) fill in the values after body.onload and fire a change event (in which case it should just work, so maybe they aren't firing an onchange).
@stephenh I don't know... But it doesn't work even if I manually click "autofill" AFTER the page has loaded, through the right-click context menu...
@drozzy Sounds like a bug in LassPass then--they should fire a change event when they do that, and then Angular will pick it up.
We're also encountering this problem with our app. With LastPass and SuperGenPass, the fields are populated, but the scope isn't updated so when the form is submitted the data passed to our API doesn't match what the user entered. In addition .change() and .trigger(input) or scope.$apply do not result in an updated scope. I suspect because the model doesn't think the data changed, but I don't know. Only solution I've found that has worked is to manually set the scope with jQuery val in the submit angular submit handler which completely overrides the data-binding, and doesn't address the actual issue.
+1 Login forms should autofill.
I came up with a better solution than my previous, but that's not saying much and it doesn't solve the main issue and may well have problems I haven't considered. In my submit handler, I update the value of each input with it's current value using $setViewValue:
$( 'input[ng-model], select[ng-model]' ).each( function() {
angular.element( this ).controller( 'ngModel' ).$setViewValue( $( this ).val() );
});
+1 works with no form-fill plugin
I urgently need a fix or elegant workaround for this. This issue makes angular.js's two-way data binding paradigm fundamentally flawed.
Changing the event which triggers the model update seems to remedy it:
https://github.com/arikal/angular.js/commit/3040148ae35e5a5302cec35e055a08daedbcb61a
YUI has a solution in its own framework - implementing its own event:
http://yuilibrary.com/yui/docs/event/valuechange.html