Many examples use the Observable.subscribe()
function in AngularIO. However, I have only seen anonymous functions being used like this:
bar().subscribe(data => this.data = data, ...);
When I try to use a function from the same class like this:
updateData(myData : DataType[]) {
this.data = data;
}
...
bar().subscribe(this.updateData, ...);
The issue is that the this
object in line 2 no longer refers to the current object. It seems to be related to some JavaScript logic that I am not familiar with. I know that you can bind an object to a function, is this what I should do? Is it considered best practice? How would one typically address this problem (I prefer not to have a lengthy anonymous function inside the subscribe()
)?