Angular表单:浏览器密码管理器

huangapple go评论50阅读模式
英文:

Angular Forms: Browser Password Manager

问题

我有一个Angular项目,版本是13,使用Angular响应式表单来实现登录页面,类似于以下内容:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'test-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
formGroup: FormGroup;

get isFormValid() {
    return this.formGroup.valid;
}
constructor(private fb: FormBuilder) {}

ngOnInit() {
    this.formGroup = this.fb.group({
        username: [null, Validators.required],
        password: [null, [Validators.required, Validators.minLength(8)]]
    });
}

}

如果用户在浏览器中保存了登录凭据并重新加载页面,则`isFormValid`仍然为`false`,直到用户在页面上任何位置点击。客户对此不满意,希望在从浏览器的密码管理器中填充凭据时启用登录按钮。
英文:

I have an Angular project, version 13, that uses the Angular Reactive Forms for the login page, something like this:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
    selector: 'test-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
    formGroup: FormGroup;

    get isFormValid() {
        return this.formGroup.valid;
    }
    constructor(private fb: FormBuilder) {}

    ngOnInit() {
        this.formGroup = this.fb.group({
            username: [null, Validators.required],
            password: [null, [Validators.required, Validators.minLength(8)]]
        });
    }
}

The login button remains disabled if isFormValid is false, and this works fine.

However, when the user saved the login credentials in the browser and reloads, isFormValid remains false until the user clicks anywhere on the page.

The client is not happy about this and would like the login button to be enabled if the credentials were populated from the browser's password manager.

答案1

得分: 1

尝试在页面的 AfterViewInit 中模拟点击。

JavaScript:

public ngAfterViewInit(): void {
    document.querySelector('body').click();
}

或者

在你的HTML输入框上加上 autocomplete="new-fieldName"

示例:

只需在你的HTML控件上加上 autocomplete="new-formControl",这样可以阻止浏览器自动填充你的输入框。

<input autocomplete="new-username" 
       formControlName="username" name="username" type="text">

<input autocomplete="new-password" 
       formControlName="password" name="password" type="password">
英文:

Try simulating a Click on the page AfterViewInit.

JavaScript :

public ngAfterViewInit(): void {
    document.querySelector(&#39;body&#39;).click();
}

Or

Put autocomplete="new-fieldName" on Your html inputs.

Example :

Just put autocomplete="new-formControl" on Your html controls which will prevent Browser to autofill Your inputs.

 &lt;input autocomplete=&quot;new-username&quot; 
            formControlName=&quot;username&quot; name=&quot;username&quot; type=&quot;text&quot;&gt;

 &lt;input autocomplete=&quot;new-password&quot; 
            formControlName=&quot;password&quot; name=&quot;password&quot; type=&quot;password&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月15日 05:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定