How to handle error in angular-ui-router's resolve

前端 未结 2 1887
闹比i
闹比i 2020-12-13 17:28

I am using angular-ui-router\'s resolve to get data from server before moving to a state. Sometimes the request to the server fails and I need to inform the use

2条回答
  •  感动是毒
    2020-12-13 18:00

    Old question but I had the same problem and stumbled on this in ui-router's FAQ section

    If you are having issues where a trivial error wasn't being caught because it was happening within the resolve function of a state, this is actually the intended behavior of promises per the spec.

    errors within resolve.

    So you can catch all resolve errors in the run phase of your app like this

    $rootScope.$on('$stateChangeError', 
    function(event, toState, toParams, fromState, fromParams, error){ 
            // this is required if you want to prevent the $UrlRouter reverting the URL to the previous valid location
            event.preventDefault();
            ... 
    })
    

提交回复
热议问题