I have data from an observable that I am trying to save into a variable for use in the template, I can see the data being returned in an array or objects which I save to this.products within the scope of the observable, however when I check this.products after executing the observable it shows as an empty array. This is the code I am using: In my component:
title = 'ngProducts';
newProduct: any;
products: any = [] ;
selectedProduct: any;
// tslint:disable-next-line:variable-name
constructor(private _httpService: HttpService) { }
// tslint:disable-next-line:use-lifecycle-interface
ngOnInit() {
this.newProduct = {details: '', category: '', brand: ''};
// tslint:disable-next-line:no-unused-expression
this.selectedProduct;
}
getProductsFromService() {
const observable = this._httpService.getProducts();
// observable.subscribe(data => console.log('this is the observable data: ', data));
observable.subscribe(data => this.products = data);
console.log('this is after fetching products: ', this.products);
}
when I console log the data inside the observable I get the array of objects, but when I try saving it to this.products it doesn't save. so the "after fetching" console log just shows an empty array.