在服务新国家从UI路由器决心函数调用访问$ stateParams [英] Access $stateParams of new state in service called from resolve function with UI Router
问题描述
有没有访问 $ stateParams
为你从一个名为下决心功能服务转变为国家的一种方式?随着 ngRoute
你会使用 $ route.current.params
。由于这个最小普拉克显示,该服务只看到你离开的状态:
Is there a way to access $stateParams
for the state you're transitioning to from a service called in a resolve function? With ngRoute
you'd use $route.current.params
. As this minimal plunk shows, the service only sees the state you're leaving:
http://plnkr.co/edit/QpwgAj?p=$p$pview
我得到了新的 $ stateParams
在 $ stateParams
直接注入下决心功能,但是值仍然落后在服务一条路径。
I get the new $stateParams
when $stateParams
is injected directly into a resolve function, but the values are still behind by one route in the service.
我是什么失踪?
推荐答案
您的不能注入的传入的参数到服务本身,而是你可以它们传递给函数在服务(或者你也可以将它们分配到一个属性在服务)。
You can't inject the the incoming parameters into the service itself, but you can pass them to a function in the service (or you could also assign them to a property in the service).
这plunker 展示了如何将它们传递给一个函数,轮流把它们存储到物业的服务。使用虚拟财产不应受到伤害,但它是一个有点难看。 :)
this plunker shows how to pass them to a function, which in turns stores them to a property in the service. Using a "dummy" property shouldn't hurt, but it is a bit ugly. :)
这可能更有意义的参数传递给控制器作为resolve属性,然后交给他们去从控制器构造服务......这就是我会做。
It might make more sense to pass the parameters to the controller as a resolve property and then hand them out to a service from the controller constructor... that's what I'd do.
这篇关于在服务新国家从UI路由器决心函数调用访问$ stateParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!