How to cancel listenglobal events?


#1

How to cancel listenglobal events?

Hi, all,
Thanks in advance,

I am writing the following code, on Ionic2,

constructor(public navCtrl: NavController, 
	public renderer: Renderer, {
	renderer.listenGlobal('window', 'message', testMethod.bind(this));
}

ionViewDidLoad() {
}

testMethod(){
	console.log('test');
}

When this page is opened for the second time or later, console.log is repeatedly called.
How can I cancel the listen event when leaving the page?


#2

Hi
listenGlobal should return a function to cancel the listener.
For your issue:

constructor(public navCtrl: NavController, 
	public renderer: Renderer, {
	this.listenerFn() = renderer.listenGlobal('window', 'message', testMethod.bind(this));
}
ionViewWillLeave () {
  this.listenerFn();
}

#3

Thank you so much!!!
Problem solved.