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

116
Vistas
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 Respuestas
    Responde la pregunta

    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 Denunciar

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