stop angular-ui-router navigation until promise is resolved

前端 未结 10 1948
情歌与酒
情歌与酒 2020-12-01 02:44

I want to prevent some flickering that happens when rails devise timeout occurs, but angular doesn\'t know until the next authorization error from a resource.

What h

10条回答
  •  星月不相逢
    2020-12-01 03:21

            var lastTransition = null;
            $rootScope.$on('$stateChangeStart',
                function(event, toState, toParams, fromState, fromParams, options)  {
                    // state change listener will keep getting fired while waiting for promise so if detect another call to same transition then just return immediately
                    if(lastTransition === toState.name) {
                        return;
                    }
    
                    lastTransition = toState.name;
    
                    // Don't do transition until after promise resolved
                    event.preventDefault();
                    return executeFunctionThatReturnsPromise(fromParams, toParams).then(function(result) {
                        $state.go(toState,toParams,options);
                    });
            });
    

    I had some issues using a boolean guard for avoiding infinite loop during stateChangeStart so took this approach of just checking if the same transition was attempted again and returning immediately if so since for that case the promise has still not resolved.

提交回复
热议问题