Home Angular1 sibling Controllers how can access each others data

# Angular1 sibling Controllers how can access each others data

Ganpat Kakar
1#
Ganpat Kakar Published in 2017-12-07 18:45:33Z
 I am trying to get FirstCtrl data in SecondCtrl, but there is no response in SecondCtrl, Please help me to solve this I Have tried to use $broadcast and$emit on $rootscope. but there is not data coming on$on 

Input is : {{firstName}}

Input should also be here: {{firstName}}

gh9
2#
 Code now compiles and runs properly. You can cut and past this into fiddler and run. 
 EDIT Addressing OPS comment. I know this method will work and it will give a correct result, but i have studied the $rootscope and event$emiter and \$broadcast will do this trick, so if you know about that then please tell me, thank you for your answer. What you want to do is a bad idea. Your method forces a tighter coupling between controllers. By working on the rootscope you are forcing all controllers to rely on a certain Item being in rootscope. This is bad because controllers are not self contained modules. By passing around a service you can decouple the controllers. Meaning that they can be used as view controllers, directive controllers, pretty much anything that requires an isolated module. Also using a service you can now cache the result, perform centralized business logic on it, and encapsulate how you get the data. This cannot be done easily on the rootscope. To sum it up, I will not show you a terrible way of doing what you want done. It is not good and will let other people whom look at this post use bad practices.