2016-06-25 13 views
0

Ich abonniere eine Observable innerhalb einer Methode und muss sie innerhalb einer anderen abbestellen. Die Methode subCounter() wird von einer init-Funktion aufgerufen und der Inhalt funktioniert einwandfrei.Angular2 Variable 'Undefined' Außerhalb Methode

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); 
} 

Wenn ich meine touchActivate() Funktion aufrufen, es enthält eine Abmelde-Funktion auf diese Variable gespeichert, jedoch wirft sie einen Fehler als this.fml.unsubscribe undefiniert ist. Die Konsolenprotokollierung this.fml kehrt in einem undefinierten Objekt zurück.

touchActivate() { 
    console.log(this.fml); 
    this.fml.unsubscribe(); 
} 

An der Spitze meiner Klasse habe ich die Variable definiert: public fml: any;

+0

Wie rufst du 'touchActivate' an? – yurzui

+0

@yurzui mit einem Ereignis-Listener für das Ereignis "touchstart". – Drakee510

+0

Können Sie uns den Code für 'touchstart' Event Listener zeigen? – yurzui

Antwort

1

Try this:

$('.range-slider').on("touchstart", this.touchActivate.bind(this)); 

oder Pfeil Funktion this behalten:

$('.range-slider').on("touchstart",() => this.touchActivate()); 
+0

Die Pfeilfunktion funktionierte perfekt für mich. Vielen Dank! – Drakee510

Verwandte Themen