A
A
Alexey K.2013-12-23 11:35:38
JavaScript
Alexey K., 2013-12-23 11:35:38

How to work with events (events) in AngularJS?

I started to pick Angular, and a question arose about events. What to do if you need to communicate with controllers that are not subordinate to each other? For such cases, there is emit and broadcast, as I understand it, but what if the controllers are equal? Hang on rootScope or create one parent controller and work through it? Tell me about the architecture, please.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
maxaon, 2013-12-23
@maxaon

You can organize communication simply through scopes.
You can place the creation in a service.
The main thing is to make sure that when you destroy your two controllers, the one created earlier is also destroyed in order to avoid memory leaks. (If necessary throughout the life of the application, create a new scope from $rootScope)

M
maxaon, 2013-12-23
@maxaon

Yes, if you do not clean up after yourself, it will be bad. But angular, at least the latest versions, took care of memory leaks, took care of it, and if you remove the scope, then all event listeners are removed.

A
Alexey K., 2013-12-23
@demiurge

those. create a service with an event manager and inject the service into the controllers?

A
Alexey K., 2013-12-23
@demiurge

jsfiddle.net/simpulton/GeAAB found how through rootScope, is this normal?

A
Alexey K., 2013-12-23
@demiurge

If anyone is interested: stackoverflow.com/questions/18856341/how-can-i-unr...
A man explains how and why a memory leak occurs if you do not "clean up after yourself"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question