Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda