Skip to main content
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Brian Rinaldi
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Brian Rinaldi

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

By
Published in Comments (15)

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

1 Comments

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.

2 Comments

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!

2 Comments

Ben,

I had this same error today, but I was already using "new" when instantiating the event emitter. It turns out I was doing it in the ngOnInit method instead of the constructor. I figured I would post in case someone else was having this issue, since it throws the same error message.

1 Comments

Thanks your sharing here. The issue is so unclear and confusing, wouldn't have been resolved until read your reminding.

1 Comments

@Dave,

Thank you and everyone else for posting this.. I too forgot to new up the event emitter and was stuck on this for an hour!

1 Comments

@Chris,

Yep - ran accross this today as well. Thanks for posting. I'm new to Angular 2 and at first I tried to instantiate it in the ngOnInit as well. Has to be a timing thing. I know @Input() values exist before the ngOnInit so I'm guessing the @Output()s need to as well.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel