7

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.

Jens
  • 5,767
  • 5
  • 54
  • 69

1 Answers1

12

I finally figured it out.

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

$state.$current.locals.globals
Jens
  • 5,767
  • 5
  • 54
  • 69
  • 1
    Gotta love the naming of `locals.globals`. This is what I came to, too... would be nice to have someone from the angular-ui-router team respond if this is the correct way to get the values. – Matt DeKrey Apr 09 '15 at 23:01
  • 1
    Since we are not using any variable prefixed with $$, which stands for "totally private and you shouldn't rely on this for compatibility", I guess it's ok to access it this way. Maybe there is a cleaner or more elegant way to do it. But I think it's safe to think this ok. – Jens Apr 28 '15 at 15:29
  • @jens Have you found a cleaner way to handle this ? No mention of this in ui-router official documentation. – Marc Dec 18 '15 at 11:16
  • @Marc No, it seems this is the way to do it for now. – Jens Dec 18 '15 at 17:55