Html ng-form 和自动完成=“关闭”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25823448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:41:54  来源:igfitidea点击:

ng-form and autocomplete="off"

htmlangularjs

提问by Knows Not Much

I have an angular form like this

我有一个像这样的角形式

<ng-form name="AddTaskForm" autocomplete="off">
   ......
</ng-form>

However when I begin entering data, chrome is still prompting me with previously entered values.

但是,当我开始输入数据时,chrome 仍然会提示我以前输入的值。

How can I prevent chrome (and all browsers) from showing any drop down on my input with previously entered values?

如何防止 chrome(和所有浏览器)使用以前输入的值在我的输入中显示任何下拉列表?

I did some search and found that people were writing custom directives, but not sure if this is really required.

我做了一些搜索,发现人们正在编写自定义指令,但不确定这是否真的需要。

enter image description here

在此处输入图片说明

回答by Luke

Despite autocompletebeing a pretty well defined part of the HTML5 spec, Chrome has flip-flopped on how they use the autocompleteproperty. Originally honoring autocomplete="off"(2013), they then decided that developers must be using it wrong and the browser should just ignore it.

尽管autocompleteHTML5 规范中定义非常明确的一部分,但Chrome 在使用该autocomplete属性的方式上却发生了翻天覆地的变化。最初是autocomplete="off"为了纪念(2013),然后他们决定开发人员一定是错误地使用了它,浏览器应该忽略它。

This doesn't mean there aren't very valid cases where you don't want the browser autofilling data (e.g. on CRM systems), but by and large, we see those as the minority cases. And as a result, we started ignoring autocomplete=off for Chrome Autofill data.

这并不意味着您不希望浏览器自动填充数据的情况非常有效(例如在 CRM 系统上),但总的来说,我们将这些视为少数情况。因此,我们开始忽略 Chrome 自动填充数据的 autocomplete=off。

(Source: Chromium bug from 2015 marked as WontFix)

(来源:2015 年的 Chromium 错误标记为 WontFix

According to the Priority of Constituencies:

根据选区优先顺序

In case of conflict, consider users over authors over implementors over specifiers over theoretical purity. In other words costs or difficulties to the user should be given more weight than costs to authors; which in turn should be given more weight than costs to implementors...

如果发生冲突,请考虑用户而不是作者而不是实现者而不是说明符而不是理论纯度。换句话说,用户的成本或困难应该比作者的成本更重要;反过来,这应该比实施者的成本更重要......

...Which leaves us developers in the unfortunate spot of finding a work-around. This article from MDNoutlines the current state well, and offers this solution of setting autocompleteto new-password:

......这让我们的开发人员陷入了寻找解决方法的不幸境地。这从MDN文章概述了目前的状态很好,并提供设定的这个解决方案autocompletenew-password

If an author would like to prevent the autofilling of password fields in user management pages where a user can specify a new password for someone other than themself, autocomplete="new-password" should be specified, though support for this has not been implemented in all browsers yet.

如果作者想防止在用户管理页面中自动填充密码字段,用户可以在其中为自己以外的人指定新密码,则应指定 autocomplete="new-password",尽管在所有浏览器呢。

I'm not sure how long this will remain valid, but for now (tested in Chrome 53 in September 2016) this is the easiest solution:

我不确定这将保持多久,但目前(2016 年 9 月在 Chrome 53 中测试)这是最简单的解决方案:

<input type="password" name="someName" autocomplete="new-password" />

Edit: Note:This has the side-effect of asking the user to save the password, possibly overwriting an existing password. So while it does "prevent the autofilling of password fields" it does not remove the element from the autocomplete mess altogether.

编辑:注意:这具有要求用户保存密码的副作用,可能会覆盖现有密码。因此,虽然它确实“防止自动填充密码字段”,但它并没有完全从自动完成混乱中删除该元素。

Edit: Updated Info:Newer versions of Chrome once again respect the autocomplete=offattribute, as Alexander Abakumov pointed out in his answer. He had it working for Chrome 68, works on Chrome 70 for me.

编辑:更新信息:正如 Alexander Abakumov 在他的回答中指出的那样,较新版本的 Chrome 再次尊重该autocomplete=off属性。他让它适用于 Chrome 68,适用于我的 Chrome 70。

回答by Alexander Abakumov

autocomplete="off"works now. Tested in the current Chrome 70.0.3538.110.

autocomplete="off"现在工作。在当前的 Chrome 70.0.3538.110 中测试。

Demo.

演示

回答by bencripps

It looks Chrome ignores the autocompleteproperty for individual inputs; the old work around was to add autocomplete=offon the entire form like you have done (which is a pretty incomplete solution as it will then add this functionality to all inputs contained in the form, which may not be desired).

看起来 Chrome 忽略了autocomplete个人的属性inputs;旧的解决方法是autocomplete=off像您所做的那样添加整个表单(这是一个非常不完整的解决方案,因为它将将此功能添加到表单中包含的所有输入中,这可能是不需要的)。

Anyway, from this postit looks like that work around is no longer available, so it looks like you may need a directive. I know this may not be what you're looking for, but I think it's your only option for chrome.

无论如何,从这篇文章看来,该解决方法不再可用,因此您可能需要一个指令。我知道这可能不是你想要的,但我认为这是你唯一的 chrome 选择。

myApp.directive('autocomplete', function() {

    return {    

        restrict: 'A',
        link: function( $scope, el, attr ) {

            el.bind('change', function(e) {

                e.preventDefault();

            }
        }
    }

});