Javascript 参数路由不会重定向到404页
我请他们帮助解决这个问题,代码如下: 控制器Javascript 参数路由不会重定向到404页,javascript,angularjs,routes,Javascript,Angularjs,Routes,我请他们帮助解决这个问题,代码如下: 控制器 angular.module('tareasApp') .controller('HumorCtrl', function ($scope, $route, $location) { $scope.pageName = $route.current.params.pageName; $scope.items =[ { href:'/humor/smile-today', img:'smile
angular.module('tareasApp')
.controller('HumorCtrl', function ($scope, $route, $location) {
$scope.pageName = $route.current.params.pageName;
$scope.items =[
{
href:'/humor/smile-today',
img:'smile.jpg',
descricao:'Those Relaxing Sounds of Waves'
}
];
});
angular.module('tareasApp')
.controller('NewsCtrl', function ($scope, $route, $location) {
$scope.pageName = $route.current.params.pageName;
$scope.items =[
{
href:'/news/news-today',
img:'news.jpg',
descricao:'Those Relaxing Sounds of Waves'
}
];
});
App.js
myApp.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
title: 'Home Page',
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/humor/:pageName', {
templateUrl: 'views/wizard.html',
controller: 'HumorCtrl'
})
.when('/news/:pageName', {
templateUrl: 'views/wizard.html',
controller: 'NewsCtrl'
})
.otherwise({
redirectTo: '/'
});
});
当我在栏后键入任何不存在的路线时,例如:
domain.com/hhhoedr
返回到起始页
问题出在包含$routeParams
的子目录中,它键入了一个不存在的页面,例如:
domain.com/humor/hhhoedr
未重定向到index.html或404.html
我想将此代码改编为我的应用程序
myApp.constant('EXISTING_PAGES', [
'page1',
'page2',
...
]);
resolve: {
exists: function ($location, $route) {
if (EXISTING_PAGES.indexOf($route.current.params.page) === -1) {
$location.path('/error/404');
}
return true;
}
}
.when('/error/404', {
templateUrl: '404.html'
})
.otherwise({
redirectTo: '/error/404'
});
如何操作?当您键入任何路由
时,例如-domain.com/hhhoedr
,它将返回起始页,因为检查代码的最后一部分,您将按如下所示进行设置
.otherwise({
redirectTo: '/'
});
.otherwise({
redirectTo: '/here give your destination path(e.g-404.html...etc)'
});
如果您将设置除all之外的任何内容。当
部分时,它将简单地重定向到起始页。请尝试根据您的要求将其设置为重定向到任何其他页
好了,现在你可以像下面这样写了
.otherwise({
redirectTo: '/'
});
.otherwise({
redirectTo: '/here give your destination path(e.g-404.html...etc)'
});
编辑我的问题。。。你知道我如何将代码调整为我的代码吗?使用$routeParams并不是那么简单。当URL中出现一些错误时,不用说$routrParams。