Passing data between controllers via a service

1,186 views
Skip to first unread message

Thiago de Castro

unread,
Nov 27, 2012, 12:08:49 PM11/27/12
to ang...@googlegroups.com
Hello!
I'm trying to pass information that is on one controller to another controller using a service for that. The service has two functions: set and get. What I notice is that when I use the set in the controller01 to the variable of the service is configured correctly.
But when I trying to get the value of this variable using the same service through the get method in the controller02 the value is empty, then it is as if the service was restarted to be used by another controller.
Here is the jsfiddle. And I need a lot of help to get this to work. Thanks in advance.

http://jsfiddle.net/KU6xV/2/

Note: I relied on this response from stackoverflow http://stackoverflow.com/questions/12008908/pass-variables-between-controllers

Pawel Kozlowski

unread,
Nov 27, 2012, 12:24:11 PM11/27/12
to ang...@googlegroups.com
Hi Thiago!

I don't know what was your exact problem since your jsFiddle has a
number of syntax errors.
After fixing JavaScript stuff it works OK as far as I can tell:
http://jsfiddle.net/D2EzR/5/

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Thiago de Castro

unread,
Nov 27, 2012, 2:11:39 PM11/27/12
to ang...@googlegroups.com
Hello Pawel!
So I checked what you did, and compared it with my code here ... I think I'm doing the same thing, probably not, since it is not working.
My code is pretty big and for better or worse show it will not help because the problem is only in this communication.
So here's a few questions that maybe can help me solve:
On fiddle is made a concatenation of the service and controls in a module of the angular ("angular.module"). And the controls are defined as follows:

controller ("name", ['$ scope', 'service name', function ($ scope, service name).

Here's the question: why are defined service name and $ scope twice? It makes a difference? I mean, in my code I define control as follows:

function 'control name' ($ scope, 'Service with Web Service', $ routeParams, 'service with another control') {code}

I call the set method during a callback done in the control 01 (in the example would FooBarController). By chance the delay of the callback would be the justification for the variable control in 02 (in the example would BarController) get empty?

For now are these two issues hehe

Thiago de Castro

unread,
Nov 27, 2012, 3:05:26 PM11/27/12
to ang...@googlegroups.com
Ok, here's something curious.
It worked. BUT, it worked because I ordered to open the another page (managed by another controller) on the same page. That is, I clicked the link and was transferred to the next page.
If I open the page (of the other controller) using the right click> open in another tab the service restarts its variables.
Reply all
Reply to author
Forward
0 new messages