Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!