You need to subscribe. Something like this (untested) ''' getUtenti() : Utente[]{ return 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); } return this.listaUtenti; }); ''' ''' getUtenti() { this.serviceUtente.getUtenti().subscribe((result) => { this.listaUtenti = result; console.log("Risultato utenti" + JSON.stringify(this.listaUtenti)); }); } '''
On Sat, May 4, 2019 at 11:55 AM <graydiamon...@gmail.com> wrote: > Thanks Kalyan for you answer, > In the end I used the async wait. Is it a good solution? > > > Il giorno sabato 4 maggio 2019 06:12:55 UTC+2, Kalyan T ha scritto: >> >> Return http from service and subscribe it in component >> >> On Fri, 3 May, 2019, 8:53 AM , <graydi...@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 ang...@googlegroups.com. >>> To post to this group, send email to ang...@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. > -- 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.