javascript - AngularJS ui-router 到一个状态但使用不同的 URL

标签 javascript angularjs angular-ui-router

在我的 AngularJS 应用程序中,我想捕获错误并将用户发送到 404 状态,如下所示:

$rootScope.$on('$stateNotFound',  function(event, unfoundState, fromState, fromParams){ 
    //console.log(unfoundState.to);
    //console.log(unfoundState.toParams);
    //console.log(unfoundState.options);
    $state.go('404');
});

这适用于用户点击应用程序中的不良链接(例如没有状态,因为有状态但没有内容的链接由其他方法处理)等。

代码加载 404 状态正常:

.state('404',
        {
            views: {
                'body': {
                    templateUrl: 'partials/404.html',
                }
            }
        });

但我想要做的是将 URL 更改为他们尝试访问的状态,因此基本上加载 404 状态但使用不正确的 URL(因为 404 会在服务器环境中工作)。

我考虑过这样做:

history.replaceState(null, null, unfoundState.to);
$state.go('404');

但这会导致应用程序出现重大错误并更改 URL,但不会更改状态!

我该怎么做?

最佳答案

try catch 与您之前的任何路由不匹配的所有 URL:

.state("404", {
    url: "*path", // will catch all URLs
    templateUrl: 'partials/404.html'
})

关于javascript - AngularJS ui-router 到一个状态但使用不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26181771/

相关文章:

javascript - insidetext 不在某些 url 中运行

javascript - 当我的javascript打开的窗口关闭时如何捕获事件

javascript - UI 路由器不显示嵌套 View

javascript - 在 Angular 项目中获取正确的 URL

javascript - 在 Particles.js 中使用 RGB 和 HSL 颜色对象

angularjs - ng-change on select 选项调用多个独特的功能

javascript - 想要使用 UI-Router 从 subview 更新父 View 值

javascript - 如何在 Angular JS 中验证状态

angularjs - angular-ui/ui-router,如何使用 $stateProvider 注入(inject)部分 View ?

javascript - Angularjs:如何在重新加载后保留复选框值