Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 713
Brian Vanderbusch Avatar asked Dec 28 '25 20:12

Brian Vanderbusch


1 Answers

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);
    }
}]);
like image 105
Brian Vanderbusch Avatar answered Dec 31 '25 10:12

Brian Vanderbusch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!