2014-10-03 118 views
2

在我的AngularJS应用程序,我想捕获错误,并且用户发送到404的状态,像这样:AngularJS UI路由器的状态,但使用不同的URL

$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,但不是国家!

我该怎么做?

+1

我还没有尝试过。我不确定'$ state.transitionTo('404',{},{location:false});'是否有效。 – 2014-10-06 14:29:16

回答

1

试试这个来捕获所有的网址不匹配任何以前的路线:

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

按照docs,我觉得你可以这样做:

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

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

相关问题