Is this the correct way to bind data using Observables? if it is, what else is wrong with my code that makes the selectedCourse variable equal undefined?
this.golfDataService
.getGolfData(id)
.subscribe(data => {
this.selectedCourse = data.data
console.log(data.data)
})
setTimeout(function () { console.log(this.selectedCourse) }, 2000);
}
I expect both console.log statements to return the same thing, but only the first console.log returns the data, which is an object with golf course information, but the console.log(this.selectedCourse) returns undefined.
setTimeout(() => { console.log(this.selectedCourse) }, 2000);if its fast enought it will return correct value. If you use function it rebindsthisto that function context not the component