0
I have method that makes one subscribe
and brings me an object array huge.
getItems() {
this.service.getItems()
.subscribe(items => { this.listaItems = items;},
() => console.log('msg de erro')
I need to assign the return to a global variable, called listaItems
right when I open my page.
so I did:
ngOnInit(){
this.getItems();
console.log(this.listaItems) // undefined
}
Yet it has returned undefined
instead of my result.
He looks like "jump" me this.getItems();
and run my console.log(this.listaItems)
first.
service:
getItems =() => this.httpService.get(`apiURl`);
Yes, it will return Undefined, pq is logging the variable, but it has not yet been filled in with the value returned by the service. If you involve the variable in a setTimeout will see that the variable needs a time to get the return!
– LeAndrade
It makes sense, since my return is great. In this case, how to expect it to be filled out and then log?
– programadorpleno
Dude you won’t be able to do anything with the variable before it has a value, and it only gets a value in the method getItems(), then you’ll have to do what you need to do there.
– LeAndrade