angularjs – 当用ui-router显示404错误页面时,如何不更改url

我想显示404错误页面,但我也想在位置保存错误的URL。

如果我会做这样的事情:

$urlRouterProvider.otherwise('404');
$stateProvider
    .state('404',{
        url: '/404',template: error404Template
    });

网址将更改为/ 404。如何在不改变实际网址的情况下在错误的网址上显示错误消息?

有这种情况的解决方案。我们会使用1)一个ui-router本机特性和2)一个配置设置。 here可以观察到 here

1)ui-router状态定义的本机特征是:

The url is not needed. State Could be defined without its representation in location.

2)配置设置为:

> otherwise() for invalid routes,哪个参数不一定是一个带有认url的字符串,但也可以是一个函数(cite):

path String | Function The url path you want to redirect to or a function rule that returns the url path. The function version is passed two params: $injector and $location

解决方案:这两个组合。我们会有没有url的状态和其他方式:

$stateProvider
  .state('404',{
    // no url defined
    template: '<div>error</div>',})

$urlRouterProvider.otherwise(function($injector,$location){
   var state = $injector.get('$state');
   state.go('404');
   return $location.path();
});

检查所有在这working example

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...