Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Ryan Anklam
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Ryan Anklam@bittersweetryan )

EXCEPTION: TypeError: Cannot Read property 'subscribe' Of Undefined In AngularJS 2 Beta 1

By Ben Nadel on

In AngularJS 2, directives can announce / trigger output events using the EventEmitter class (ng.core.EventEmitter). Each output must get its own instance of the EventEmitter class, which means that you must instantiate the EventEmitter when you are implementing your output event stream. If you forget to "new" the EventEmitter, such as typing:

this.myEvent = ng.core.EventEmitter();

... you will get the following error:

EXCEPTION: TypeError: Cannot read property 'subscribe' of undefined.

Notice that I forgot to "new" the EventEmitter into existence. Invoking it in this way downgrades the EventEmitter() constructor into a simple "function call". The fix is easy - just add "new":

this.myEvent = new ng.core.EventEmitter();

I've tripped over this like at least 4 or 5 times already this week; so, I figured I'd just make a record of it incase anyone also runs into the error and tries to Google for the error message.




Reader Comments

@Lars,

Thanks for the feedback. I have 2 other befuddling errors that I think I'll write up for reference.

Reply to this Comment

Thanks for this - Saw this error when i set up an Output without wiring it up and had no idea why the error was appearing or what it meant.

Reply to this Comment

What a waste of an hour, running around trying to figure out where I was trying to call 'subscribe'. Course, this makes complete sense!! Thanks!

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.