2016-06-25 2 views
0

Я подписываюсь на наблюдаемый в рамках метода и должен отказаться от него в другом. Метод subCounter() вызывается из функции init, и содержимое работает нормально.Angular2 Variable 'Undefined' Внешний метод

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

Когда я называю свою функцию touchActivate(), он содержит отписки функция этой переменной хранится, однако он выдает ошибку, как this.fml.unsubscribe не определено. Ведение журнала консоли this.fml возвращает неопределенный объект.

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

В верхней части моего класса я переменная определила: public fml: any;

+0

Как вы называете метод touchActivate'? – yurzui

+0

@yurzui с прослушивателем событий для события «touchstart». – Drakee510

+0

Не могли бы вы показать нам код для прослушивателя событий touchStart? – yurzui

ответ

1

Попробуйте это:

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

Или использовать функцию стрелки, чтобы сохранить this:

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

Функция стрелки отлично работала для меня. Спасибо! – Drakee510