angular-route-segment icon indicating copy to clipboard operation
angular-route-segment copied to clipboard

Access route params in resolve function

Open troygilbert opened this issue 11 years ago • 2 comments

The $routeParams don't seem to be populated when the resolve step is being performed. So, inside my resolve function, I can't access these route params. It seems to me the natural use case for the resolve function would be using the route params to resolve/load something? Am I thinking about this wrong?

My scenario: I have a page with an ID in the URL, I want to grab a resource off the server based on that ID before loading the page. So I'm doing this:

$routeSegmentProvider.when('/accept-invite/:token', 'accept-invite')
        .segment('accept-invite', {
            templateUrl: 'partials/signin/accept-invite.tpl.html',
            controller: 'AcceptInviteCtrl',
            resolve: {
                invite: function(Auth, $routeSegment) {
                    return Auth.getInvite($routeSegment.$routeParams.token);
                }
            }
        });

Auth.getInvite() returns an $http.post() promise.

troygilbert avatar Jul 07 '14 21:07 troygilbert

Yes, $routeSegment.$routeParams is populated only when the segment is switched, it is the very meaning of it -- to preserve the state of $routeParams of the currently active segment, not the one which is being activated. While resolving, you might need to use the original $routeParams instead of $routeSegment.$routeParams:

            resolve: {
                invite: function(Auth, $routeParams) {
                    return Auth.getInvite($routeParams.token);
                }
            }

artch avatar Jul 23 '14 11:07 artch

Unfortunately, it doesn't seem as if $routeParams is populated when the resolve functions are called. Seems like a huge loss to not have this functionality.

troygilbert avatar Nov 10 '14 20:11 troygilbert