I'm trying to get an array of objects from an API call and save it to a component variable. However, when assigning in a function, the value stays local. For example:
export class ItemsComponent implements OnInit {
items: Item[];
constructor(private ProductInformation: ProductInformationService) { }
ngOnInit() {
this.getProducts();
console.log(this.items); //undefined
this.items = MOCK_ITEMS;
console.log(this.items); //correct 3 items
}
getProducts() {
this.ProductInformation.getAllProducts(
(response) => {
this.items = response
console.log(this.items); //correct 3 items
}
);
}
}
Why is items
undefined after calling getProducts()
? Why does it only get saved locally, and how can I get it to work?