Is this possible that I can set autocomplete "off" at any one place and it restricts all the inputs of the application in Angular(8 or Later)?
Asked
Active
Viewed 273 times
0
-
1Does this answer your question? [Is there a W3C valid way to disable autocomplete in a HTML form?](https://stackoverflow.com/questions/582244/is-there-a-w3c-valid-way-to-disable-autocomplete-in-a-html-form) – Elias Soares Jul 20 '21 at 17:49
1 Answers
4
Using directive selector we can match all the input element. Then using HostingBinding we can set autocomplete off. In this way we don't need to apply directive on input element manully as it will be applied automatically.
import { Directive, HostBinding } from '@angular/core';
@Directive({
selector: 'input'
})
export class InputDirective {
@HostBinding('autocomplete') autocomplete = 'off';
constructor() {}
}

Chellappan வ
- 23,645
- 3
- 29
- 60
-
I have used the same but it is not working, do I need to add something else too. It is not working for me. – Gaurav Panwar Jul 21 '21 at 08:04
-
if you are creating directive using angular cli then you need to tweak your directive selector. Then make sure you have added directive inside declarion array in app.module.ts – Chellappan வ Jul 21 '21 at 08:15
-
Hey Chellappan, I am not using angular CLI. I have simply created the above directive and using inside the declarations array in app.module.ts – Gaurav Panwar Jul 21 '21 at 08:59
-
Then it should work. Can you inspect your input element and see whether autocomplete attributes added or not – Chellappan வ Jul 21 '21 at 09:18
-