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

229
Vistas
Escucha angular para almacenar cambios y emitir un valor de un componente de servicio a un componente diferente, solo después de que el servicio complete ciertas operaciones

Aquí tenemos las clases GetDataAsyncService que espera el cambio en la tienda (y no ejecuta el bloque de código debajo de él hasta un cambio en la tienda ( this.getDataAsyncService.getAsyncData().subscribe((data)=>{ )} ). Cuando se llama desde MainComponent , obtendrá el retorno de (propA); (de GetDataAsyncService) antes de que se ejecute el bloque de código en el oyente, porque el oyente todavía está esperando un cambio en la tienda. Quiero emitir solo ese observable cuando se ejecuta ese bloque de operación.

 export class GetDataAsyncService { propA; constructor(private store: Store<AppState>) getData():Observable<any>{ this.store.pipe(select(appState)).subscribe((val)=>{ // operation block // some operations // some more operations this.propA = val.propA; }) return of(propA); // this should be emitted with the latest value only when the block of code above executes - not before that } } export MainComponent implenents OnInit{ propA: string = ''; constructor(private getDataAsyncService: GetDataAsyncService){} ngOnInit(): void{ this.getDataAsyncService.getAsyncData().subscribe((data)=>{ this.propA = data.propA; }) } // any operation involving propA // code ...... }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Puede lograrlo devolviendo el Observable mismo desde la función getData y asignándolo al accesorio requerido, en lugar de subscribe a él, como se muestra a continuación:

 export class GetDataAsyncService { propA; constructor(private store: Store<AppState>) {} getData(): Observable<any> { return this.store.pipe( select(appState), map((val) => val.propA) ); } } export class MainComponent implements OnInit { propA: string = ''; constructor(private getDataAsyncService: GetDataAsyncService) {} ngOnInit(): void { this.getDataAsyncService.getAsyncData().subscribe((propA) => { this.propA = propA; }); } }
about 4 years ago · Juan Pablo Isaza 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