如何显示具有UI路由器404错误页面时,不改变网址 [英] How not to change url when show 404 error page with ui-router

查看:161
本文介绍了如何显示具有UI路由器404错误页面时,不改变网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示404错误页面,而且我想保存错误的URL中的位置。

I want to show 404 error page, but also I want to save wrong url in location.

如果我会做这样的事情:

If I'll do something like that:

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

URL将变为 / 404 。我怎么能显示出在不改变实际的URL上错误的URL错误信息?

url will change to /404. How I can show error message on wrong urls without changing actual url?

推荐答案

有对这种情况的解决方案。我们会用1)有一个 UI路由器内置功能,2)一个配置设置。一个工作示例可以观察到的这里

There is solution for this scenario. We'd use 1) one ui-router native feature and 2) one configuration setting. A working example could be observed here.

1)的UI路由器状态定义一个土生土长的特点是:

1) A native feature of ui-router state definitions is:

是不需要的 URL。国家可以在没有它的位置重新presentation来定义。

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

2)的配置设置是:

  • otherwise() for invalid routes, which parameter does not have to be a string with default url, but could be a function as well (cite):

路径的字符串|功能要重定向的URL路径或返回URL路径的函数规则。功能版本传递两个参数:$喷油器和$位置

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

解决方案:这两者的结合。我们将有状态 网​​址和自定义的,否则

Solution: combination of these two. We would have state without url and custom otherwise:

$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();
});

检查所有在这个工作示例

这篇关于如何显示具有UI路由器404错误页面时,不改变网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆