除了将它们注入控制器之外,还有其他方法可以访问已解决的状态依赖项吗? [英] Is there a way to access resolved state dependencies besides injecting them into the controller?

查看:13
本文介绍了除了将它们注入控制器之外,还有其他方法可以访问已解决的状态依赖项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从服务或指令访问已解析的数据,以便在整个应用程序中执行一些常规操作.

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;
});

我明白了:

"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.

我还发现可以从状态访问resolve对象:

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.

我只需要访问解析的值,就像访问数据属性或 $state.params 一样.

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

推荐答案

我终于想通了.

可以通过 $state.$current 对象访问所有已解析的数据:

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

$state.$current.locals.globals

这篇关于除了将它们注入控制器之外,还有其他方法可以访问已解决的状态依赖项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆