Search code examples
cordovaaddeventlistenerionic3inappbrowser

Analog removeEventListener ionic 3


In ionic 3 addEventListener code: browser.addEventListener('loadstop', callback) not working and analog is: browser.on('loadstop').subscribe(()=>{}) But how i can reformat this code browser.removeEventListener('loadstop', callback) to ionic 3?


Solution

  • I was facing the same issue when trying to subscribe to push notification event and unfortunately in my case unsubscribe() method did not work.

    The solution I found and prefer especially when I have multiple subscriptions in one page and I want to unsubscribe from them when I leave the page, is the following:

    Import the RxJs operator:

    import 'rxjs/add/operator/takeWhile';
    

    Define a variable, say liveSubscription: boolean = true; in your component.

    Then subscribe like this:

    browser.on('loadstop')
    .takeWhile(() => this.liveSubscription)
    .subscribe(() => {})
    

    When you want to unsubscribe, simply set this.liveSubscription = false;.