简体   繁体   English

当浏览器预先填充表单字段时,AngularJS“ng-submit”有问题吗?

[英]AngularJS “ng-submit” has problems when the browser pre-populates form fields?

I'm fairly new to AngularJS, and am experimenting with a single-page application having a "login" form. 我是AngularJS的新手,我正在尝试使用“登录”表单的单页应用程序。 The form is bound by "ng-submit", and its controller makes an AJAX call returning a token if authentication was successful. 表单由“ng-submit”绑定,并且如果身份验证成功,其控制器会发出一个AJAX调用返回一个令牌。 Subsequent AJAX calls pass this token. 后续的AJAX调用会传递此令牌。 (No, I DON'T want to use basic auth, because I want a non-hacky "logout" button). (不,我不想使用基本身份验证,因为我想要一个非hacky“注销”按钮)。

I have setup my username and password fields with "required", so that AngularJS will display a tooltip when users try to submit the form with a blank value in a field: 我已将用户名和密码字段设置为“required”,以便当用户尝试在字段中提交带有空白值的表单时,AngularJS将显示工具提示:

<form name="loginForm" ng-submit="login()">
    <fieldset>
        <legend>Sign In</legend>
        <label>Email</label>
        <input name="loginEmail" type="text" placeholder="Registered email address…" ng-model="loginEmail" required>
        <label>Password</label>
        <input name="loginPassword" type="password" placeholder="Password…" ng-model="loginPassword" required>
        <br/>
        <button type="submit" class="btn">Login</button>
    </fieldset>
</form>

The issue arises when some browsers (Firefox, at least) asks whether the user wants the browser to remember the username and password, and pre-populate it next time. 当某些浏览器(至少是Firefox)询问用户是否希望浏览器记住用户名和密码,并在下次预先填充它时,会出现问题。

When the browser populates either of these fields, AngularJS basically stops working. 当浏览器填充其中任何一个字段时,AngularJS基本上停止工作。 The "ng-submit"-bound form will not submit... the bound controller function isn't invoked at all. “ng-submit”绑定表单将不会提交...根本不会调用绑定控制器函数。 My first thought was that the pre-populated fields didn't trigger an event, so AngularJS thinks they're still blank. 我的第一个想法是,预先填充的字段没有触发事件,因此AngularJS认为它们仍然是空白的。 However, there are no tooltips popping up to warn about blank fields. 但是,没有弹出工具提示来警告空白字段。 It's like AngularJS just completely shut down. 就像AngularJS完全关闭一样。

Strangely, as soon as you make ANY manual edit to either field, AngularJS comes back to life... validation tooltips and form submission start working again. 奇怪的是,只要你对任一字段进行任何手动编辑,AngularJS就会恢复生机......验证工具提示和表单提交再次开始工作。

Is there a bug here, or is the problem with lack of knowledge on my end? 这里有错误,还是我的知识缺乏问题? How can you make AngularJS recognize browser-populated fields? 如何让AngularJS识别浏览器填充的字段? Or if there are problems in this area, how can you prevent the browser from pre-populating fields so they don't interfere with AngularJS? 或者,如果此区域存在问题,您如何阻止浏览器预先填充字段,以免它们干扰AngularJS?

$(window).load(function() {
  // updates autofilled fields
  window.setTimeout(function() {
    $('input[ng-model]').trigger('input');
  }, 100);
});

The issue comes from the fact the the browser doesn't send any events on auto fill. 问题来自于浏览器不会在自动填充时发送任何事件。 It was reported as an issue on Angular's BT. 它被报道为Angular的BT问题。

https://github.com/angular/angular.js/issues/1460 https://github.com/angular/angular.js/issues/1460

But I doubt it will be fixed from Angular since its more a browser issue. 但我怀疑它将从Angular修复,因为它更多的是浏览器问题。

So the solution is to do some dirty hacks like setting a timer and set the model's state to 'dirty' or trigger events from the form. 所以解决方案是做一些肮脏的黑客攻击,比如设置一个计时器并将模型的状态设置为'脏'或从表单触发事件。

You can see some attempts here AngularJS browser autofill workaround by using a directive 您可以通过使用指令在此处看到AngularJS浏览器自动填充解决方法的一些尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM