question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ionic 2: Events publish/subscribe loses context

See original GitHub issue

Short description of the problem:

When subscribing to an Event, providing inline code preserves context, but providing a function loses context.

Given a class function like this:

setFlag() { /* do some stuff here */  }

This works:

this.events.subscribe('hello', ()=>{ this.setFlag(); });

But this fails, with no such function setFlag:

this.events.subscribe('hello', this.setFlag );

In another component:

this.events.publish('hello');

This is problematic, because there’s no way to unsubscribe from inline code.

What behavior are you expecting?

I would expect the context to be preserved in the message handler.

Which Ionic Version? 1.x or 2.x

2 beta 11

Run ionic info from terminal/cmd prompt: (paste output below)

Cordova CLI: 6.3.0 Gulp version: CLI version 3.9.0 Gulp local: Local version 3.9.1 Ionic Framework Version: 2.0.0-beta.11 Ionic CLI Version: 2.0.0-beta.36 Ionic App Lib Version: 2.0.0-beta.19 ios-deploy version: 1.8.3 ios-sim version: 5.0.4 OS: Mac OS X El Capitan Node Version: v4.2.2 Xcode version: Xcode 7.3.1 Build version 7D1014

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
arsenslyusarchukcommented, Aug 23, 2016

@thomaswb Using publish/subscribe u have to explicitly bind context or use arrow function like this

this._logoutSub = () => {
  this.goToLoginPage();
};
this.events.subscribe('user:loggedOut', this._logoutSub);
1reaction
kensodemanncommented, Oct 17, 2017

it would be really helpful if someone could point me to a good tutorial or other explanation about the difference between arrow functions and non-arrow functions in this particular context

I believe your issue simply boils down to how this is handled in JavaScript, so hopefully the following explanation will help:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ionic 2 Events, publish and subscribe not working
I realize why it doesn't show on console.. Page 2 has to be loaded and ready before Page 1 initiate an event. If...
Read more >
Everything You Need To Know About Publish/Subscribe
Learn everything you need to know about Pub/Sub, a message exchange pattern that scales by decoupling senders and receivers.
Read more >
Angular Reactive Forms: Tips and Tricks | by Netanel Basal
With every article I publish, my goal is to help you become a better and more confident coder. Here are a couple of...
Read more >
Pull subscriptions | Cloud Pub/Sub Documentation
In a pull subscription, a subscriber client requests messages from the Pub/Sub server. The pull mode can use one of the two service...
Read more >
access value in subscribe outside of subscription angular 2
You just can't return the value directly because it is an async call. An async call means it is running in the background...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found