'How to call a method of a class from an rxjs observable subscription

Could I please ask:

This code works fine:

class MyClass
{
  constructor()
  {
    this.m_observable = Rx.Observable.of(1, 2);
    
      this.m_observable.subscribe(
                                 function (x) {alert(x);}
                                 );
  }
  
  DoIt(x)
  {
    alert(5 * x);
  }
}

When I create a new instance of the class I see "1" then "2".

I would like to call the DoIt method of the class when the subscription fires, this code does not work, I see no alerts:

class rxjsShapeShifter
{
  constructor()
  {
    this.m_observable = Rx.Observable.of(1, 2);
    
      this.m_observable.subscribe(
                                 this.DoIt;
                                 );
  }
  
  DoIt(x)
  {
    alert(5 * x);
  }
}

How can I call that method from the observable subscription?

Thanks for any help.



Solution 1:[1]

I think you have typo in your subscribe. The second code of your should be:

    constructor() {
      this.m_observable = Rx.Observable.of(1, 2);
      this.m_observable.subscribe(this.DoIt);
    }
    
    DoIt(x) {
      alert(5 * x);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HassanMoin