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

情到浓时终转凉″ 提交于 2019-12-21 12:06:25

问题


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.


回答1:


I finally figured it out.

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

$state.$current.locals.globals


来源:https://stackoverflow.com/questions/28026620/is-there-a-way-to-access-resolved-state-dependencies-besides-injecting-them-into

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!