是否有访问除了他们注入控制器解决状态依赖性的方法吗? [英] 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.
这是测试数据我设置:
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.
我还发现,我可以从国家获得的决心对象:
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.
推荐答案
我终于想通了。
这是可能通过$状态访问所有的数据解决当前$对象:
It's possible to access all the resolved data through the $state.$current object:
$state.$current.locals.globals
这篇关于是否有访问除了他们注入控制器解决状态依赖性的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!