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。 –