How can I send my $scope
object from one controller to another using .$emit
and .$on
methods?
function firstCtrl($scop
How can I send my $scope object from one controller to another using .$emit and .$on methods?
You can send any object you want within the hierarchy of your app, including $scope.
Here is a quick idea about how broadcast and emit work.
Notice the nodes below; all nested within node 3. You use broadcast and emit when you have this scenario.
Note: The number of each node in this example is arbitrary; it could easily be the number one; the number two; or even the number 1,348. Each number is just an identifier for this example. The point of this example is to show nesting of Angular controllers/directives.
3
------------
| |
----- ------
1 | 2 |
--- --- --- ---
| | | | | | | |
Check out this tree. How do you answer the following questions?
Note: There are other ways to answer these questions, but here we'll discuss broadcast and emit. Also, when reading below text assume each number has it's own file (directive, controller) e.x. one.js, two.js, three.js.
How does node 1 speak to node 3?
In file one.js
scope.$emit('messageOne', someValue(s));
In file three.js - the uppermost node to all children nodes needed to communicate.
scope.$on('messageOne', someValue(s));
How does node 2 speak to node 3?
In file two.js
scope.$emit('messageTwo', someValue(s));
In file three.js - the uppermost node to all children nodes needed to communicate.
scope.$on('messageTwo', someValue(s));
How does node 3 speak to node 1 and/or node 2?
In file three.js - the uppermost node to all children nodes needed to communicate.
scope.$broadcast('messageThree', someValue(s));
In file one.js && two.js whichever file you want to catch the message or both.
scope.$on('messageThree', someValue(s));
How does node 2 speak to node 1?
In file two.js
scope.$emit('messageTwo', someValue(s));
In file three.js - the uppermost node to all children nodes needed to communicate.
scope.$on('messageTwo', function( event, data ){
scope.$broadcast( 'messageTwo', data );
});
In file one.js
scope.$on('messageTwo', someValue(s));
HOWEVER
When you have all these nested child nodes trying to communicate like this, you will quickly see many $on's, $broadcast's, and $emit's.
Here is what I like to do.
In the uppermost PARENT NODE ( 3 in this case... ), which may be your parent controller...
So, in file three.js
scope.$on('pushChangesToAllNodes', function( event, message ){
scope.$broadcast( message.name, message.data );
});
Now in any of the child nodes you only need to $emit the message or catch it using $on.
NOTE: It is normally quite easy to cross talk in one nested path without using $emit, $broadcast, or $on, which means most use cases are for when you are trying to get node 1 to communicate with node 2 or vice versa.
How does node 2 speak to node 1?
In file two.js
scope.$emit('pushChangesToAllNodes', sendNewChanges());
function sendNewChanges(){ // for some event.
return { name: 'talkToOne', data: [1,2,3] };
}
In file three.js - the uppermost node to all children nodes needed to communicate.
We already handled this one remember?
In file one.js
scope.$on('talkToOne', function( event, arrayOfNumbers ){
arrayOfNumbers.forEach(function(number){
console.log(number);
});
});
You will still need to use $on with each specific value you want to catch, but now you can create whatever you like in any of the nodes without having to worry about how to get the message across the parent node gap as we catch and broadcast the generic pushChangesToAllNodes.
Hope this helps...