1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice and clean, just like it's supposed to be. Not sure how I missed this. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.