stop angular-ui-router navigation until promise is resolved

I know this is extremely late to the game, but I wanted to throw my opinion out there and discuss what I believe is an excellent way to “pause” a state change. Per the documentation of angular-ui-router, any member of the “resolve” object of the state that is a promise must be resolved before the state is finished loading. So my functional (albeit not yet cleaned and perfected) solution, is to add a promise to the resolve object of the “toState” on “$stateChangeStart”:

for example:

$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
    toState.resolve.promise = [
        '$q',
        function($q) {
            var defer = $q.defer();
            $http.makeSomeAPICallOrWhatever().then(function (resp) {
                if(resp = thisOrThat) {
                    doSomeThingsHere();
                    defer.resolve();
                } else {
                    doOtherThingsHere();
                    defer.resolve();
                }
            });
            return defer.promise;
        }
    ]
});

This will ensure that the state-change holds for the promise to be resolved which is done when the API call finishes and all the decisions based on the return from the API are made. I’ve used this to check login statuses on the server-side before allowing a new page to be navigated to. When the API call resolves I either use “event.preventDefault()” to stop the original navigation and then route to the login page (surrounding the whole block of code with an if state.name != “login”) or allow the user to continue by simply resolving the deferred promise instead of trying to using bypass booleans and preventDefault().

Although I’m sure the original poster has long since figured out their issue, I really hope this helps someone else out there.

EDIT

I figured I didn’t want to mislead people. Here’s what the code should look like if you are not sure if your states have resolve objects:

$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
    if (!toState.resolve) { toState.resolve = {} };
    toState.resolve.pauseStateChange = [
        '$q',
        function($q) {
            var defer = $q.defer();
            $http.makeSomeAPICallOrWhatever().then(function (resp) {
                if(resp = thisOrThat) {
                    doSomeThingsHere();
                    defer.resolve();
                } else {
                    doOtherThingsHere();
                    defer.resolve();
                }
            });
            return defer.promise;
        }
    ]
});

EDIT 2

in order to get this working for states that don’t have a resolve definition you need to add this in the app.config:

   var $delegate = $stateProvider.state;
        $stateProvider.state = function(name, definition) {
            if (!definition.resolve) {
                definition.resolve = {};
            }

            return $delegate.apply(this, arguments);
        };

doing if (!toState.resolve) { toState.resolve = {} }; in stateChangeStart doesn’t seem to work, i think ui-router doesn’t accept a resolve dict after it has been initialised.

Leave a Comment