我正在使用JavaScript,Express Server和angular编写主页。目前,我有以下路由:
function config($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/login/login.view.html',
controller: 'loginCtrl',
controllerAs: 'vm'
})
.when('/home', {
templateUrl: '/home/home.view.html',
controller: 'homeCtrl',
controllerAs: 'vm'
})
.otherwise({redirectTo: '/'});
}
而不是.other中的重定向到登录页面,我希望用户获得一个类似于以下页面的页面:
未找到
在此服务器上找不到请求的URL
。
如何在重定向页面的控制器中获取请求的URL?或实现此目的的常用方法是什么?
亲切的问候,沃尔夫冈
您可以为错误页面创建新的路由,并在用户导航到无效路由时重定向到/error
路由。
function config($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/login/login.view.html',
controller: 'loginCtrl',
controllerAs: 'vm'
})
.when('/home', {
templateUrl: '/home/home.view.html',
controller: 'homeCtrl',
controllerAs: 'vm'
})
.when('/error', {
template: `
<h3>Not Found</h3>
<p>The requested URL is not found</p>
`
})
.otherwise({redirectTo: '/error'});
}
以上解决方案使用静态模板。如果需要,您还可以使用控制器并执行逻辑以显示特定的错误。