Ned
Ned

Reputation: 4161

Sharing resolved data between controllers - UI Router

I'm using UI Router as a router for my application, and I have a situation where I need to have all instances of one resource available all the time. These instances are listed in sidebar of my application. I starter with something like this in my routes.js file:

    .state('auth', {
       url: '/',
       abstract: true,
       controller: RootCtrl,
       template: '<div ui-view autoscroll="true"></div>',
       resolve: {
          campaigns: function(UserCampaignsCollection, activeUser) {
             return (new UserCampaignsCollection).query({user_id:  activeUser.id});
          }
       }
    })

This means that I had to create separate controller, just for keeping all the campaigns and sharing those between other controllers, like this:

_app.controller('RootCtrl',
  function($rootScope, campaigns) {
    $rootScope.campaigns = campaigns;
});

This works fine, since I resolve it on only one place, and then all of my other states just inherit base state (as auth.account, auth.inovice etc.), but I would like to avoid need for attaching all of my campaigns on rootScope.

Is there some other way to pass data to other controllers, which would not involve creating dummy controller like this, and attaching data to rootScope?

Upvotes: 1

Views: 447

Answers (1)

Bartosz Gościński
Bartosz Gościński

Reputation: 1580

If all other states inherit from auth then (via https://github.com/angular-ui/ui-router/wiki/Nested-States-and-Nested-Views#what-do-child-states-inherit-from-parent-states) their controllers can also be injected with resolved campaigns.

Upvotes: 1

Related Questions