Angularjs 转到错误状态,并将URL保留为生成错误的状态

Angularjs 转到错误状态,并将URL保留为生成错误的状态,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我用它来捕捉错误并重定向到错误页面 module.run(function ($state, $rootScope, translateService) { $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) { $state.go("app.error"); }); }); 但这会移动到错误状态并更改URL。我

我用它来捕捉错误并重定向到错误页面

module.run(function ($state, $rootScope, translateService) {
    $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
        $state.go("app.error");
    });
});
但这会移动到错误状态并更改URL。我想转到错误页面并保留指向错误页面的URL

我尝试过使用location选项,
$state.go(“app.error”,null,{location:false})
,但它保留了我来自的州的URL,而不是有错误的州的URL


我是否可以转到错误状态并保留URL,或者转到错误状态,然后将URL更改回
toState
的URL?

我通过将错误信息发送到错误状态,然后将URL设置为$location.path解决了这一问题

module.run(function ($state, $rootScope) {
    $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
        $state.get("app.error").$error = {
            toState: toState,
            toParams: toParams,
            fromState: fromState,
            fromParams: fromParams,
            message: error.message,
            stack: error.stack
        };

        $state.go("app.error");
    });
});

module.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider.state("app.error", {
        url: "/error",
        resolve: {
            error: function () {
                return this.self.$error;
            }
        },
        templateUrl: "app/error.tpl.html",
        controller: function ($state, $scope, $log, $location, error) {
            if (error) {
                $location.path($state.href(error.toState, error.toParams).replace(/^#/, ""));
                $log.error(error.message, error.stack);
            }

            $scope.error = error;
        }
    });
});

我通过将错误信息发送到错误状态,然后将url设置为$location.path来解决这个问题

module.run(function ($state, $rootScope) {
    $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
        $state.get("app.error").$error = {
            toState: toState,
            toParams: toParams,
            fromState: fromState,
            fromParams: fromParams,
            message: error.message,
            stack: error.stack
        };

        $state.go("app.error");
    });
});

module.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider.state("app.error", {
        url: "/error",
        resolve: {
            error: function () {
                return this.self.$error;
            }
        },
        templateUrl: "app/error.tpl.html",
        controller: function ($state, $scope, $log, $location, error) {
            if (error) {
                $location.path($state.href(error.toState, error.toParams).replace(/^#/, ""));
                $log.error(error.message, error.stack);
            }

            $scope.error = error;
        }
    });
});

我通过将错误信息发送到错误状态,然后将url设置为$location.path来解决这个问题

module.run(function ($state, $rootScope) {
    $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
        $state.get("app.error").$error = {
            toState: toState,
            toParams: toParams,
            fromState: fromState,
            fromParams: fromParams,
            message: error.message,
            stack: error.stack
        };

        $state.go("app.error");
    });
});

module.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider.state("app.error", {
        url: "/error",
        resolve: {
            error: function () {
                return this.self.$error;
            }
        },
        templateUrl: "app/error.tpl.html",
        controller: function ($state, $scope, $log, $location, error) {
            if (error) {
                $location.path($state.href(error.toState, error.toParams).replace(/^#/, ""));
                $log.error(error.message, error.stack);
            }

            $scope.error = error;
        }
    });
});

我通过将错误信息发送到错误状态,然后将url设置为$location.path来解决这个问题

module.run(function ($state, $rootScope) {
    $rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
        $state.get("app.error").$error = {
            toState: toState,
            toParams: toParams,
            fromState: fromState,
            fromParams: fromParams,
            message: error.message,
            stack: error.stack
        };

        $state.go("app.error");
    });
});

module.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider.state("app.error", {
        url: "/error",
        resolve: {
            error: function () {
                return this.self.$error;
            }
        },
        templateUrl: "app/error.tpl.html",
        controller: function ($state, $scope, $log, $location, error) {
            if (error) {
                $location.path($state.href(error.toState, error.toParams).replace(/^#/, ""));
                $log.error(error.message, error.stack);
            }

            $scope.error = error;
        }
    });
});

我认为这就是诀窍(我不久前发现了这个诀窍,它没有被记录在案):


看看关于$stateChangeError的部分,我认为这就是诀窍(我不久前发现了这个诀窍,它没有被记录在案):


看看关于$stateChangeError的部分,我认为这就是诀窍(我不久前发现了这个诀窍,它没有被记录在案):


看看关于$stateChangeError的部分,我认为这就是诀窍(我不久前发现了这个诀窍,它没有被记录在案):


查看关于$stateChangeError的部分要添加到已接受的答案中,我做了一个小更改,允许我保留错误url,但在用户尝试刷新时尝试转到主页(或任何您想要的状态):

controller: function ($state, $scope, $log, $location, error) {
        if (error) {
            $log.error(error.message, error.stack);
        }else{
            $state.go('app'); // or app.home or whatever
        }

        $scope.error = error;
    }

为了添加到已接受的答案中,我做了一个小更改,允许我保留错误url,但在用户尝试刷新时尝试转到主页(或任何您想要的状态):

controller: function ($state, $scope, $log, $location, error) {
        if (error) {
            $log.error(error.message, error.stack);
        }else{
            $state.go('app'); // or app.home or whatever
        }

        $scope.error = error;
    }

为了添加到已接受的答案中,我做了一个小更改,允许我保留错误url,但在用户尝试刷新时尝试转到主页(或任何您想要的状态):

controller: function ($state, $scope, $log, $location, error) {
        if (error) {
            $log.error(error.message, error.stack);
        }else{
            $state.go('app'); // or app.home or whatever
        }

        $scope.error = error;
    }

为了添加到已接受的答案中,我做了一个小更改,允许我保留错误url,但在用户尝试刷新时尝试转到主页(或任何您想要的状态):

controller: function ($state, $scope, $log, $location, error) {
        if (error) {
            $log.error(error.message, error.stack);
        }else{
            $state.go('app'); // or app.home or whatever
        }

        $scope.error = error;
    }

等等,对不起。这仅适用于直接url导航,如使用href或直接浏览器位置栏更新。不是用$state.go.等等,抱歉。这仅适用于直接url导航,如使用href或直接浏览器位置栏更新。不是用$state.go.等等,抱歉。这仅适用于直接url导航,如使用href或直接浏览器位置栏更新。不是用$state.go.等等,抱歉。这仅适用于直接url导航,如使用href或直接浏览器位置栏更新。不是用$state.go。