angularjsangular-ui-router

Is there a way to access resolved state dependencies besides injecting them into the controller?


I need to access resolved data from a service or directive to do some general operations across the whole application.

The only way to I seem to be able to access resolved data is injecting it into the controller.

This is the test data I setup:

resolve: {
    test: function() {
         console.log("resolving");
        return 5+2;
    }
}

I have tried this in my controller just to see what happens, but it doesn't work:

$injector.invoke(function(test) {
    console.log("injected", test);
    $scope.test = test;
});

I get:

"Error: [$injector:unpr] Unknown provider: testProvider <- test

So it seems that resolved data is passed as locals to the invoke function on the state controller.

I also found out that I can access the resolve object from the state:

$state.current.resolve

But this is the raw resolve object without the resolved data. I could invoke those functions to resolve the data, but I would be resolving dependencies all over again. If there were any requests on the resolve object they would be called twice.

I just need to access the resolved values just like I would access data attributes or the $state.params.


Solution

  • I finally figured it out.

    It's possible to access all the resolved data through the $state.$current object:

    $state.$current.locals.globals