Sharing data between two controllers

2,749 views
Skip to first unread message

J Castillo

unread,
Sep 15, 2012, 8:52:33 PM9/15/12
to ang...@googlegroups.com
I'm trying to pass multiple fields to another controller via service using only one object. Unfortunately am getting stuck looping through with angular. Which is the best way to map field 1 to field 1 and field 2 to field 2, etc. etc?

Here is an example of what I'm trying to do.


I also have an separate question which is relate to ng-repeat and expressions.

I'll like to know if there is a way to do an expression where you use the ng-repeat but only display the content if the index of the array I'm looping through is 2 or whatever number I choose.

Example:

<span ng-repeat="item in items | item:"secondAddress""> </span>

Thank you!

Johan

unread,
Sep 15, 2012, 11:52:42 PM9/15/12
to ang...@googlegroups.com
Have not looked your fiddle problem but for ng-repeat you can reference the index and some other count values as per docs:

So you could do 

<span ng-show="$index ==2"> ... </span>

Johan

unread,
Sep 15, 2012, 11:56:07 PM9/15/12
to ang...@googlegroups.com
For your controller problem see:  http://onehungrymind.com/angularjs-communicating-between-controllers/ 


On Sunday, September 16, 2012 12:52:33 PM UTC+12, J Castillo wrote:

J Castillo

unread,
Sep 17, 2012, 9:37:55 PM9/17/12
to ang...@googlegroups.com
Thanks Johan but I don't want to follow this example to pass data between controllers using $rootScope since I heard from Misko and other people that is not the right way of doing things.

Any alternatives?

Johan

unread,
Sep 17, 2012, 9:59:14 PM9/17/12
to ang...@googlegroups.com
Examples is not using $root - it is using events.

You have 3 options:

1. $root scope

2. Events

3. Shared service

If you search this forum you'll find past discussions about it.

Guillaume Lecomte

unread,
Sep 25, 2012, 6:39:46 AM9/25/12
to ang...@googlegroups.com
Events are only availables on scopes so it is $(root)Scope anyway isn't it? The exemple inject rootScope in the service...

Peter Bacon Darwin

unread,
Sep 25, 2012, 7:46:25 AM9/25/12
to ang...@googlegroups.com
Hi Guillaume,
Passing data via $rootScope is basically like having objects in a global namespace, to which everyone has access.  This can lead to problems where you don't know what changed your data.
The Shared Service (i.e. a singleton object) is better as you have control over access to the data within the service and also components must request to have the service injected into them so it is easier to see who is messing with the data.
The event strategy, is a different idea, where your component can raise events that may or may not be handled by someone else.  It provides nice independence between components but doesn't provide a clear central data source - although this can be achieved with a service that exposes events.  It is true that the scope is used to pass events (broadcast down the tree, emit up the tree) but this is simply a transport mechanism rather than using the scope as a global dumping ground for all your data.  By the way, to get events to go to everyone you broadcast them from the rootScope.

Pete

--
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.
 
 

Guillaume Lecomte

unread,
Sep 25, 2012, 12:45:05 PM9/25/12
to ang...@googlegroups.com
Thanks I get your point but I'm not convinced with the "Events only" technique when it comes to share data, you have to write extra code because you can't $watch service data or use it directly in angular views ( + personnal opinion:  emitting events on the rootScope has the same namespace problem than attaching a shared data object to it (events needs names) )

I've looked at the GoogleChrome / textdrive-app source code trying to see what would be the "clean" way for doing this and it looks like they found it:
They are just attaching the "tabs" service to the controllers scopes (not the rootScope) from the controllers init code, https://github.com/GoogleChrome/textdrive-app/blob/master/app/js/controllers/StatusBar.js, so the controllers decide what will be the service namespace in their scope, sounds like a good pattern to me, 
you just have to be very carefull about what you expose in your shared services since it will be inspected in the $digest cycles.

Side note: some "best practices" projects written or reviewed by the Angular team would be nice to address this kind of questions :)
Reply all
Reply to author
Forward
0 new messages