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:
是否有一种方法可以访问正在从解析函数中调用的服务转换到的状态的$stateParams ?对于ngRoute,你需要使用$route。current.params。正如这个最简单的提示所示,该服务只看到您要离开的状态:
http://plnkr.co/edit/QpwgAj?p=preview
http://plnkr.co/edit/QpwgAj?p=preview
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.
当将$stateParams直接注入到解析函数时,我将获得新的$stateParams,但该值仍然被服务中的一条路由隐藏。
What am I missing?
我缺少什么?
2 个解决方案
#1
11
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).
您不能将传入的参数注入服务本身,但是您可以将它们传递给服务中的一个函数(或者您也可以将它们分配给服务中的一个属性)。
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. :)
这个柱塞显示了如何将它们传递给函数,函数将它们存储到服务中的属性。使用“虚拟”属性应该不会有什么坏处,但它有点丑陋。:)
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.
将参数作为解析属性传递给控制器,然后将它们从控制器构造函数中传递给服务,这可能更有意义。这就是我做的。
#2
8
In your resolve function in stateprovider
在stateprovider的解析函数中
resolve: {
something: function(MyService, $stateParams){
return MyService.doSomethingWithParam($stateParams.someParameter);
}
}
In your service
在你的服务
myService = angular.module('myService', [])
.factory('MyService', function() {
var myServiceInstance;
myServiceInstance.doSomethingWithParam= function(param){
//...store or do something with your state param here and return result
};
return myServiceInstance;
});
#1
11
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).
您不能将传入的参数注入服务本身,但是您可以将它们传递给服务中的一个函数(或者您也可以将它们分配给服务中的一个属性)。
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. :)
这个柱塞显示了如何将它们传递给函数,函数将它们存储到服务中的属性。使用“虚拟”属性应该不会有什么坏处,但它有点丑陋。:)
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.
将参数作为解析属性传递给控制器,然后将它们从控制器构造函数中传递给服务,这可能更有意义。这就是我做的。
#2
8
In your resolve function in stateprovider
在stateprovider的解析函数中
resolve: {
something: function(MyService, $stateParams){
return MyService.doSomethingWithParam($stateParams.someParameter);
}
}
In your service
在你的服务
myService = angular.module('myService', [])
.factory('MyService', function() {
var myServiceInstance;
myServiceInstance.doSomethingWithParam= function(param){
//...store or do something with your state param here and return result
};
return myServiceInstance;
});