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

386
Vistas
Not load data onClose dialog ? using Angular

I want to trigger load data ( which also load on init ) when user close dialog.

in AddUser-component.ts i have any logic and only important part

  public onClose: Subject<any> = new Subject<any>();


  this.addService.addNewUser(user).subscribe(res => {
   this.onClose.next(res.body)
   this.bsModalRef.hide();
 } 

and when trigger this in main component AllUser-components.ts i have

public addNewUser() {
    this.bsModalRef = this.modalService.show(AddUserComponent); 
    (this.bsModalRef.content as AddUserComponent).onClose.subscribe(singleUserFromChildComponent)=>{
        this.loadUser(); // HERE IS PROBLEM... when i console.log this.allUsers i don't see new added user .....
        if (bill) {
            this.lastAddedItem(this.allUsers, singleUserFromChildComponent);  
        }
        this.bsModalRef.hide()
    })
}

and above i have loadUser

private loadUser() {
    this.service.getAllUsers( 
    ).subscribe((res) => {
        this.allUsers = res.body!; 
    })
}

this.allUsers var is not updated....not load new user.... why ? I load data and allUsers need to be updated with new data

 this.allUsers = res.body!; 

but not... i don't know why ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Well its because the loadUser() method is asynchronous, and while you are loading it on modal close, it does not finish yet (also having nested subscriptions is a kind of bad practice, you can read more in that thread Why nested subscription is not good?)

What I can suggest you is to remove the subscribe invocation from loadUser method and return just an observable:

  private loadUser() {
    return this.service.getAllUsers().pipe(map((res: any) => res.body!),tap((res: any) => this.allUsers = res));
}

In your OnInit (or constructor method initialize allUsers in this way.

this.loadUser().pipe(first()).subscribe();

and then refactor your modal close part with the switchMap( it will wait untill your service.getAllUsers finished)

public addNewUser() {
  this.bsModalRef = this.modalService.show(AddUserComponent); 
    (this.bsModalRef.content as AddUserComponent).onClose
    .pipe(tap(singleUserFromChildComponent => this.singleUserFromChildComponent = singleUserFromChildComponent), switchMap(addedUser => (this.loadUser()))).subscribe(_ => {
      if (bill) {
        this.lastAddedItem(this.allUsers, this.singleUserFromChildComponent);  
    }
    this.bsModalRef.hide()
    });
}

also please add the lastAddedUser property to your component

public singleUserFromChildComponent: any;
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