I have declared the data I want from the API using observable in a separate class. I want to access the data declared in the separate class into my component class. But javascript being asynchronous in nature will give me undefined
once the control crosses the function call of the observable method. So is there a way to 'subscribe' to this function that already contains an observable that is fetching data or any other way of accessing data from getData() in GetDataClass.ts into my Component class?
Here's the code:
GetDataClass.ts
Data: any;
getData() {
this.dataservice.getAllData()
.subscribe(response => {
this.temp = response;
this.Data = this.temp.ReturnData.DataList;
console.log('this.data new: ', this.Data);
}, err => console.log('error: ', err))
}
AppComponent
:
data: any;
constructor(private dataService: GetDataClass ...){
this.dataService.getData();
this.data = this.dataService.temp
}
}