Brian Vanderbusch
Brian Vanderbusch

Reputation: 3339

Angular-UI Resolve Promise object inside $urlRouterProvider.when()

I'm trying to set up some basic application data inside the module.config() object, and I only do so when the user hits the app with a unique key attached to a specific entry point, otherwise my states will resolve with different responses.

I'm using $urlRouterProvider.when(), to catch the user at the specified entry point, then resolve the promise ($http.get()), then return the state. But can't seem to make it work. The data itself does return, but the function isn't returning the path, and I can't figure out why.

$urlRouterProvider.when('/hi/:userkey',function($match,$rootScope, $http){
        $http.get('http://api.com/login/'+$match.userkey)
            .success(function(result){
                $rootScope.data = result;
                  return '/home';
            })

        });

I've also tried setting the return value inside .then():

.then(function(){
    return '/home';
})

I'm not getting any errors, but the return value isn't being processed.

Upvotes: 3

Views: 1202

Answers (2)

Brian Vanderbusch
Brian Vanderbusch

Reputation: 3339

So, I don't feel like this is the best answer, but I've been asked by a few now if I found a solution, and I did get it working by activating the desired state instead of returning it's route. I don't want to accept this answer however, as I feel esalija's answer should work, and I'm trying to put the test together to see if it's actually a bug with ui.router. Until then, this definitely works, and off the top of my head, at least for a small app, it's not costing you much.

$urlRouterProvider.when('/hi/:userkey',function($match, $http, Api, $state){
        return $http.get('http://api.site.com/index.php/login/'+ $match.userkey).success(function(result){

            Api.userkey = $match.userkey;
            Api.appdata = result;
           
        }).then(function(){
            $state.go('app');
        });

You can try it out on my devsite/#/hi/5300e0ce9dfb8

Edit

I suppose I should feel that dirty doing it this way, it's the same way it's accomplished by the module itself:

$urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
    if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) {
        $state.transitionTo(state, $match, false);
    }
}]);

Upvotes: 2

Esailija
Esailija

Reputation: 140228

You are not returning the promise

$urlRouterProvider.when('/hi/:userkey', function ($match, $rootScope, $http) {
    return $http.get('http://api.com/login/' + $match.userkey)
        .then(function (result) {
            $rootScope.data = result;
            return '/home';
        });
});

Upvotes: 1

Related Questions