UI-Router 1.X - how to handle exceptions thrown fr

2019-09-02 05:18发布

问题:

Exception in resolve function while transitioning to a new state in angularjs

I've a code to transition to new state on a button click, but one of the resolve functions is failing due to some error at the server. How and where should I handle this exception. I've tried the method described in this link https://github.com/angular-ui/ui-router/issues/1783, but it didn't help. I'm not sure if I'm doing it right or not. This is what my code looks like after taking it from the link, upon setting a break point and debug my code but I don't see the break point hitting on exception from the resolve function. Please correct me what am I doing wrong here or guide me in the right direction to get this resolved.

window.app = angular.module('app', ['di'])

app.run(['$rootScope',function($rootScope){
    $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
        if(toState.name == 'Categories')
        {
            // Do Something
        }
    });
}])

Route setting code

{
    name: 'app.setting',
    url: '/settings',
    abstract: true,
    component: 'settingDetails',
    resolve: {
        getData: ($stateParams,
        $http)=>$stateParams.locationGroupId?$http.get(`/ui/getData/${
            $stateParams.id
        }/`).then(response=>response.data): [],

    }
}

回答1:

my ui router version 1.0.0-rc.1.

With UI-Router 1.X, state change events are deprecated, DISABLED and replaced by Transition Hooks. Instead of listening for events, use the Transition Hook API.

For more information, see UI-Router Guide - 1.0 Migration - State Change Events