0

I am using UI router to define several states and their child views. I set their controllers accordingly as well.

I'd like one or more of those controllers to be active before the view is loaded, because of data fetching and some other operations.

Is this possible? Is this a good/bad idea?

.state(stateNames.MY_STATE, {
                        url: '/products/:view',
                        views: {
                            'items-of-count@my.state': {
                                templateUrl: 'path/to/template.html',
                                controller: 'NotYetLoadedController'
                            },
                            'selected-items-count@my.state': {
                                templateUrl: 'path/to/this/template.html',
                                controller: 'NotYetLoadedController'
                            }
                        },
                        params: {
                            view: {
                                squash: true
                            }
                        }
                    })
binarygiant
  • 6,362
  • 10
  • 50
  • 73
  • 1
    Controller is related to the view. Only when view comes into play, Controller could be initiated. And will live only through the View/state lifetime. What you should do is 1) to use resolve as a trigger related to state 2) use services which are singletons to keep the data longer 3) use the .run() phase to trigger some preload which will be needed later – Radim Köhler Apr 03 '15 at 15:29
  • 1
    I would probably setup a service that does a initial request from the $resource and cache that data. Services will be available before the controllers that want to leverage them. – techie.brandon Apr 03 '15 at 15:34

0 Answers0