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

126
Visualizações
How do i refresh the data i retrieve from a service?

I want to display the list of available rooms in a

    list in my home component.

    for that I declared rooms variable that i assigned to a subscription to the websocketService. However whenever i created a new room, it's not added to the list automatically unless i refresh the page. how do i fix that?

    home.component.ts

       rooms : any;
          constructor(private webSocketService: WebSocketService, private formBuilder: FormBuilder,            private router: Router)
            { }
          ngOnInit(): void { 
            this.rooms = this.webSocketService.getRooms().subscribe(res => {
              console.log(res.body)
             });
           }
    

    webSocketService :

    public getRooms() :  Observable<any> {
          return this.http.get(`${this.url}/rooms`,{observe: 'response'});
    

    home.html :

    <ul>
      <li *ngFor="let room of rooms">{{rooms}}</li> 
    </ul>
    
    about 4 years ago · Juan Pablo Isaza
    2 Respostas
    Responde à pergunta

    0

     ngOnInit(): void { 
           this.webSocketService.getRooms().subscribe(res => {
              this.rooms = res
             });
           }
    

    You need to make the assignment in the class variable inside the subscription where it becomes available

    about 4 years ago · Juan Pablo Isaza Relatório

    0

    Try by converting you variable rooms as observable and just get the reference from the service. In the template You will get a subscription by using Async Pipe.

    ngOnInit(): void { 
      this.rooms$ = this.webSocketService.getRooms();
    }
    
    <ul>
      <li *ngFor="let room of rooms$ | async">{{room.someProperty}}</li> 
    </ul>
    
    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