Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
How to get the form status after rendering (FormBuilder)

I am using FormBuilder to generate form. On ngOnInit, I am calling a function which generates a login form. If any user saved their credentials, next time when they try to login these fields will be pre-filled with their saved credentials (username and password).

I want to get the status of form after rendering. I called ngAfterViewInit lifecycle hook but got INVALID status of the form even it has username and password are pre-filled.

Below is the code:

constructor(private formBuilder: FormBuilder) {}

ngOnInit() {
  this.generateForm();
}

generateForm() {
  this.loginForm = this.formBuilder.group({
    username: [null, [Validators.required]],
    password: [null, [Validators.required]],
 });    
}

ngAfterViewInit() {
  console.log(this.loginForm.status);   //INVALID
}

Didn't understand why it is showing INVALID. Is there any other lifecycle hook in which I can get the VALID status of the form if it's pre-filled?

Looking forward for the solution. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's not possible to know the autofilled values until the user interacts with the page. (security feature of Chrome)

You can try this workaround and have a reference to the fields:

 @ViewChild('usernameField')
 usernameField: ElementRef;

 @ViewChild('passwordField')
 passwordField: ElementRef;

 ngAfterViewInit(): void {
     setTimeout(() => this.setValues(), 200);
 }

 setValues(): void {
this.loginForm.get('login').setValue(this.usernameField.nativeElement.value);
this.loginForm.get('password').setValue(this.passwordField.nativeElement.value);
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda