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

387
Visualizações
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 Respostas
Responde à pergunta

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 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