I have a 'messages' factory that will query my database for a list of messages.

I'm using the list of messages in two different places. Once to add a message count indicator, and then once to show a list of messages. Since I'm injecting the service into two different controllers, it seems like it's creating two instances of my factory, and hitting the database twice for the list.

How would I set things up to only ask for the list once, and use the list for both display and count purposes in both controllers?

My factory looks like this:

```
myApp.factory('messagesService', [
    '$rootScope',
    function($rootScope) {
        var messages = [];

        function query() {
            // Would actually hit the database asyncronously
            messages = ['one', 'two', 'three', 'four'];
            console.log('query');
            $rootScope.$emit('messages.update');
        }

        function all() {
            return messages;
        }

        return {
            query: query,
            all: all
        }
    }
]);

```

My controllers are using blocks like this to watch for changes:

```
$rootScope.$on('messages.update', function() {
    $scope.messagesCount = messagesService.all().length;
});

```

But it means i need a `messagesService.query();` in each controller for things to be reliable.

So here are a few jsFiddle examples of it as I have things now:

1. Doesn't work (only updates the header): <http://jsfiddle.net/TSLfc/1/>
2. Works but would break if I didn't load the dashboard controller:
<http://jsfiddle.net/TSLfc/2/>
3. Works every time, but queries the server twice:
<http://jsfiddle.net/TSLfc/3/>

Is there a better way to organize my code? Should I build out the messages factory into it's own full module?