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

316
Visualizações
How to extract data from Observable?

How can extract the data to pass it to the form?

user$!: Observable<UserI>

ngOnInit(): void {
  this.user$ = this.userService.getPerson();
  this.initializeForm();
}

initializeForm(): void {
  this.form = this.fb.group({
    fullName: new FormControl(?, Validators.required),
  });
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have multiple options:

You could initialize the form when the data is loaded or you could initialize the form with empty/default values and set update the values when the data is loaded.

Method 1:

ngOnInit(): void {
  this.userService.getPerson().pipe(
      tap(user => this.initializeForm(user))
  ).subscribe();
}

initializeForm(user: UserI): void {
  this.form = this.fb.group({
    fullName: new FormControl(user.fullName, Validators.required),
  });
}

Method 2:

ngOnInit(): void {
  this.initializeForm();
  this.userService.getPerson().pipe(
      tap(user => {
          this.form.get('fullName').setValue(user.FullName);
      })
  ).subscribe();
}

initializeForm(): void {
  this.form = this.fb.group({
    fullName: new FormControl('', Validators.required),
  });
}

Personally I prefer to create fields for my controls:

readonly fullName = new FormControl('', Validators.required);

ngOnInit(): void {
  this.initializeForm();
  this.userService.getPerson().pipe(
      tap(user => {
          this.fullName.setValue(user.FullName);
      })
  ).subscribe();
}

initializeForm(): void {
  this.form = this.fb.group({
    fullName: this.fullName,
  });
}

Then I can access them directly, including in html

<input [formControl]="fullName"/>
<span *ngIf="fullName.errors?.required">Fullname is required</span>
over 4 years ago · Santiago Trujillo 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