【问题标题】:AngularJS ui-router to a state but use different URLAngularJS ui-router 到一个状态但使用不同的 URL
【发布时间】:2014-10-11 07:57:24
【问题描述】:

在我的 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');
});

这适用于当用户点击应用程序中的错误链接(例如没有状态,有状态但没有内容的链接由 else 方法处理)等时。

代码加载404状态正常:

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

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

我正在考虑这样做:

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

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

我该怎么做?

【问题讨论】:

  • 我没试过。我不确定$state.transitionTo('404',{},{location:false}); 是否有效。

标签: javascript angularjs angular-ui-router


【解决方案1】:

试试这个来捕获所有与你之前的路由不匹配的 URL:

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

【讨论】:

    【解决方案2】:

    根据docs,我认为你可以这样做:

    $state.go('404',{},{location:false});
    

    注意:我正在创建一个 plunkr 来验证,但我现在没有时间完全完成它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2016-06-03
      相关资源
      最近更新 更多