问题在于浏览器的自动填充没有触发正确的事件,以便 Angular 可以绑定到它。
一种解决方案是为您的输入触发更改事件,这是我从此处自定义的指令(http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/ http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/):
咖啡脚本:
angular.module('app')
.directive 'formAutoFillFix', ->
(scope, elem, attrs) ->
# Fixes Chrome bug: https://groups.google.com/forum/#!topic/angular/6NlucSskQjY
elem.prop 'method', 'POST'
# Fix autofill issues where Angular doesn't know about autofilled inputs
if attrs.ngSubmit
setTimeout ->
elem.unbind('submit').bind 'submit', (e) ->
e.preventDefault()
elem.find('input').triggerHandler('change')
scope.$apply attrs.ngSubmit
, 0
JavaScript:
angular.module('app').directive('formAutoFillFix', function() {
return function(scope, elem, attrs) {
// Fixes Chrome bug: https://groups.google.com/forum/#!topic/angular/6NlucSskQjY
elem.prop('method', 'POST');
// Fix autofill issues where Angular doesn't know about autofilled inputs
if(attrs.ngSubmit) {
setTimeout(function() {
elem.unbind('submit').bind('submit', function(e) {
e.preventDefault();
elem.find('input').triggerHandler('change');
scope.$apply(attrs.ngSubmit);
});
}, 0);
}
};
});
该网站上发布的解决方案使用 jQuery,而上述解决方案不依赖它。
像使用任何其他指令一样使用它:
<form role="form" ng-submit="login()" form-auto-fill-fix>