Communication between controllers - angular

I have nested controllers and I need to establish a connection between the controllers. There are two ways I can think of achieving this:

  • You have a central PubSub service that all controllers can use for communication. The disadvantage of this in my opera is that each controller can talk to any other controller, and there is no point for a hierarchy.
  • Another approach is that the dispatcher can only speak with the father and with these children, as described below: enter image description here

Therefore, I need a separate communication channel for each level of the hierarchy. Not sure how to achieve this. I was thinking of creating a service that provides a communication channel, but how can I insert it into the appropriate controllers?

, - ? , , ?

+4
3

,

, , , 1, - com. , .

0

2 .

/ , $scope.$broadcast('myEvent', args) , $scope.on('myEvent', function(event, args){...}).

, , - /, $scope.$emit.

: $scope. $emit and. $on angularJS , $emit () $on. $broadcast ().

0

postaljs : http://jonathancreamer.com/an-angular-event-bus-with-postal-js/

You can use the mailbox by posting messages on specific channels. This way you can separate your messages from things like root, root.leve1, root.level2 and subscribe to it using a template. ie: root *, root.level1, ...

0
source

Source: https://habr.com/ru/post/1525376/


All Articles