0

I need to pass a value from two controllers to the third controller using a button click. I tried google searching but couldn't find any result. Can any one help me. Please explain clearly as I am new to angular js. Thanks in advance

jasna
  • 61
  • 3
  • 10
  • possible duplicate of [Angular: Share data between controllers](http://stackoverflow.com/questions/21919962/angular-share-data-between-controllers) – Julien Sep 11 '14 at 09:37

1 Answers1

0

There are two solutions that will work here.

  1. Use a service. A service is a singleton ie only one instance is created and shared between whatever you have it injected into. So if your service is shared between two or more controllers, an update in one will also reflect in the other

  2. Create a custom event. use $emit or $broadcast depending on whether you want to go up or down the scope tree and then use $on to catch the event. You can pass variables as part of the event

Cathal
  • 1,740
  • 18
  • 31
  • So this means only one or more values from a single controller can be passed to different controllers and many values from multiple controllers cannot be passed to a single controller. Am I right? – jasna Sep 11 '14 at 09:47
  • you can pass whatever you like. create a DataStore service and inject it into your controllers. in a one controller DataStore.x = 1, DataStore.y = 2 etc. These variables are now available anywhere else the service is injected – Cathal Sep 11 '14 at 09:49
  • check this out: https://egghead.io/lessons/angularjs-sharing-data-between-controllers – Cathal Sep 11 '14 at 12:21