How to listen to a request at Angular 4

Asked

Viewed 96 times

0

I’m making an HTTP request in Angular 4, but I need to listen to it and show it in the template in real time, that is to say, as long as the information is automatically registered in the grid.

ngOnInit() {

    this.userService.lista()
    .subscribe(data => {  this.usuarios = data });
}

this and the return of the request on the component can help me?

  • This is only when it starts the template. To add as it is inserted, you can do it from the front or, in the method save of your application, the return of it you push users for example. To answer more accurately, would need more details...

2 answers

0

Create an attribute on the page to make the Property bind:

@Input() meuAtributo: any;// o tipo você que define

this.userService.lista()
    .subscribe( (data: any) => {
       this.meuAtributo = data;
        console.log(this.meuAtributo, data);
    });
<h2 [(ngModel)]="meuAtributo"></h2>

0

Good morning, you can use an interval

atualizarDados(){
  this.userService.lista()
 .subscribe(data => {  this.usuarios = data });
}

window.setInterval(atualizarDados, 600);

This will make every 600 ms this routine be called ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.