Return http from service and subscribe it in component

On Fri, 3 May, 2019, 8:53 AM , <graydiamon...@gmail.com> wrote:

> Hi everyone,
> I have recently entered the Angular world and I am running into a problem
> that I really can't solve.
>
> I created a service called UsersService where inside I invoke a rest
> service that returns the list of registered users.
>   getUtenti() : Utente[]{
>     let data =  this.http.get(this.API).subscribe((data) => {
>       let output = data['utenti'];
>       for (var i = 0; i < output.length; i++){
>         let utente = <Utente> output[i];
>         this.listaUtenti.push(utente);
>       }
>     });
> I call this method from a component.ts
> getUtenti(){
>   this.listaUtenti = this.serviceUtente.getUtenti();
>     console.log("Risultato utenti" +JSON.stringify(this.listaUtenti));
>
> The getUtents does not wait for the "this.serviceUtente.getUsenti ()" to
> end and consequently the User list will always be empty.
> In fact if I put a timeout to my getUsers it works.
>
>  setTimeout(() => {
>       console.log("Risultato utenti" +JSON.stringify(this.listaUtenti));
>   }, 300);
>
> I was wondering, is there a way to solve this problem or do I have to play
> with event emitters? Thanks a lot to everyone
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Angular and AngularJS discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+unsubscr...@googlegroups.com.
> To post to this group, send email to angular@googlegroups.com.
> Visit this group at https://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to