The autocomplete behaviour of browsers is linked to the name
attribute of your control. This issue is actually not related to Angular, is more a browser issue, Google Chrome is particularly hard to deal with on the subject.
Solution 1:
The following solution may work, however at least once Google Chrome stopped supporting it, the value that should work is autocomplete="off"
, if not please try the second solution:
<!-- removed everything but the changes for the sake of simplicity -->
<mat-form-field ...>
<input
...
autocomplete="off"
/>
</mat-form-field>
Solution 2:
This is the one I ended up using for my project as it worked in every scenario, you can trick the browser to think there's nothing to autocomplete by removing any trace of the original name
attribute:
// typescript
untraceableFieldName: string;
fixChromeAutocomplete(): void {
this.untraceableFieldName = 'locatorId' + new Date().getTime().toString();
}
<!-- removed everything but the changes for the sake of simplicity -->
<mat-form-field ...>
<input
...
[name]="untraceableFieldName"
(keydown)="fixChromeAutocomplete()"
(change)="fixChromeAutocomplete()"
/>
</mat-form-field>
The last solution has of course the issue that now you'll not be able to use the name
attribute for any of your logic anymore, this solution is not elegant, but it sure works.
You can read a lot more about the issue at this other question.
Specially for Google Chrome:
Sept/2021: Lately their bug report has been more active for related bugs as it seems that it is a regression issue. Is actually one of the most ranked bug to solve at Chrome bugs tracker. You can see that issues related to Chrome ignoring the autocomplete="off"
still open here. So, currently the second solution seems to be the only way to solve it.
autocomplete="new-password"
? As it seems working just fine. I'll write an explanation if it worked for you. – Ruddautocomplete="off"
seems to be working for me, another option would be to check if you actually have imported MatAutocompleteModule viaimport {MatAutocompleteModule} from '@angular/material/autocomplete';
into your angular project and listed it in your@NgModule
- and finally to look into your browser settings under autofill/autocomplete and turn it off. Can you provide the code where you've imported it? – Tea