In one of my methods, I am subscribing to an observable and later need to unsubscribe from it in another method. The subCounter() method is triggered from an initialization function and works correctly.
subCounter() {
this.fml = this.playerService.counter(this.song).subscribe(data => {
this.pos = data;
this.time.position = Math.round(this.pos);
this.time.dur = this.song.duration - this.time.position;
this.time.durMinutes = Math.floor(this.time.dur / 60);
this.time.durSeconds = ('0' + Math.ceil(this.time.dur - this.time.durMinutes * 60)).slice(-2);
this.time.posMinutes = Math.floor(this.time.position / 60);
this.time.posSeconds = ('0' + Math.ceil(this.time.position - this.time.posMinutes * 60)).slice(-2);
this.time.percent = this.time.position / this.song.duration * 100;
})
// This works just fine
console.log(this.fml);
}
However, when I call the touchActivate() function, which contains an unsubscribe method for the variable stored, it throws an error because this.fml.unsubscribe
is undefined. Logging this.fml
returns an undefined object.
touchActivate() {
console.log(this.fml);
this.fml.unsubscribe();
}
At the beginning of my class, I have the variable declared as:
public fml: any;