I am embedding an Angular reactive forms login page. The login button is disabled if the form is not valid.
import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'signin', templateUrl: './signin.component.html' }) export class SignInComponent implements OnInit { private signInForm: FormGroup; constructor(private formBuilder: FormBuilder) { } ngOnInit() { this.buildForm(); } private buildForm(): void { this.signInForm = this.formBuilder.group({ userName: ['', [Validators.required, Validators.maxLength(50)]], password: ['', [Validators.required, Validators.maxLength(50)]] }); this.signInForm.valueChanges .subscribe((data: any) => this.onValueChanged(data)); this.onValueChanged(); } private onValueChanged(data?: any) { console.log(data); }
So, when I launch it in the browser, I have the pre-populated "username" and "passwords" fields. And in the console, I have the values '{username: "email@email.com", password: ""}', and as a result the "login" button is disabled. But if I click somewhere on the page, it launches onValueChanged , and I see '{username: "email@email.com", password: "123456"}', and the "login" button is turned on.
If I go incognito. I don’t have any pre-filled fields (they are empty), but when I fill out (select) the values, in the console I see '{username: "email@email.com", password: "123456"}' and the "login" button is turned on without an extra click.
Maybe these are different events? Autofill and autofill? And does Angular work with them differently?
What is the best way to solve it? And why is the onValueChanged function executed only once when the browser autofill fields?
autocomplete angular autofill typescript
A. Gladkiy
source share