0
我请他们帮忙解决这个问题,下面的代码:参数路由不被重定向到页面404
控制器
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: '/'
});
});
当我键入任何在酒吧后不存在的路线,例如:
doma in.com/hhhoedr
返回到开始页面。
的问题是在子目录,其中包含了$routeParams
,键入页面不存在,如:
domain.com/humor/hhhoedr
没有进行重定向index.html或404.html。
我想修改此代码I found in another answer到我的应用程序。
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'
});
我该怎么办?
编辑我的问题......你知道我怎么可以调整此代码,[我在另一个答案发现(http://stackoverflow.com/questions/24862225/page-doesnt-redirect-to-the -specified-page-on-invalid-url),我的代码? –
使用$ routeParams并不那么简单。当URL中有一些错误时,不用说$ routrParams。 –