有没有办法全局关闭 Material 的 MatInput
字段的自动完成行为?我想摆脱这个到处重复的样板代码:
<input matInput formControlName="myCtrl" autocomplete="off" />
例如类似于在应用程序模块的提供程序数组中使用注入(inject) token 全局定义表单字段的外观和标签选项:
// Default appearance of material form fields
{ provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: { appearance: 'fill' } },
// Globally disable label of material form fields
{ provide: MAT_LABEL_GLOBAL_OPTIONS, useValue: { float: 'never' } }
我扫描了文档和源代码,但找不到任何东西。
最佳答案
有点晚了,但也许会有帮助。
您只需添加这个简单的指令即可:
import { Directive, HostBinding, Attribute } from '@angular/core';
@Directive({
selector: '[matInput]',
})
export class AutocompleteOffDirective {
@HostBinding('attr.autocomplete') auto;
constructor(@Attribute('autocomplete') autocomplete: string) {
this.auto = autocomplete || 'off'
}
}
如果您选择覆盖自动完成属性,它将从元素中获取文本。
关于angular - 全局关闭 Material matInput 上的自动完成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59300424/