如何将参数从 ngRoute 传递给解析对象内的函数?

How can I pass an argument from ngRoute to a function inside the resolve object?

我有以下路线并使用函数 all_data 解析对象:

.when('/events', {
    controller: eventCtrl,
    templateUrl: 'some/path.html',
    resolve:{
        all_data: function(){

        }

我的问题是,如果我像这样将一些参数传递给路由:

.when('/events/:param1'), {
....

我能否以某种方式将此 "param1" 传递到 "all_data" 函数中,如下所示:

....
resolve: {
all_data: function(param1){
    console.log(param1);
}
...

?

假设您使用的是 ngRoute 并且 url 类似于 '/events/:param1'

,您可以像这样编写解析方法
all_data: ['$routeParams', function($routeParams){
    console.log($routeParams.param1);
}

如果您使用 ui-router,请使用 $stateParams 而不是 $routeParams

Note that the $routeParams are only updated after a route change completes successfully. This means that you cannot rely on $routeParams being correct in route resolve functions. Instead you can use $route.current.params to access the new route's parameters.

-- AngularJS $routeParams API Reference