RSSAmplifier

magarcia · Jul 3, 2016

Events in Angular2

0
Sign in to vote or save

magarcia

During the feed component migration, I encountered code I did not know how to port to Angular 2:

When the condition is true, the directive listens for muted.byRequest, muted.byUser, and muted.Join events. The event-handling code is straightforward (ignoring $watch for now).

But, wait a minute, I have read the documentation of Angular 2 like a hundred times and I don't remember nothing about "events" with Angular 1.X style. That's because it not exist. Angular 2 don't have a way to make events like in Angular 1, so I have to find a solution. After a search for a solution, I found this entry in laco's blog.

Broadcaster

Basically, the idea is to make a service that implements the $broadcast and $on a method as we had in $rootScope. To do this we use Observables, very importants in Angular 2, and for this case, we use a Subject.

So, now we can start to use events like in the example:

How I solved the problem?

I didn't. These events are only to show the user information pop-ups about when he is muted, so it's not a critical feature. By now these events are fired and listen in different components, and some of it still implemented in Angular 1.4.

This is a solution I want to share with you, but I'm not sure if this will be the way that I will use to solve the problem. Because these events probably won't be necessary when I reimplement the MuteNotifier.

Read the original on magarcia.io

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.