2015-05-09 116 views
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' 
     }); 

我該怎麼辦?

回答

0

當您輸入任何路線e.g-domain.com/hhhoedr時,它將返回到起始頁面,因爲檢查您的代碼的最後一部分,您正在像下面那樣設置它。

.otherwise({ 
     redirectTo: '/' 
     }); 

如果您將設置任何東西,除了從您的所有.when部分將簡單的重定向到起始page.Try將其設置爲重定向到其他頁面按您的要求。

好的,現在你可以像下面這樣寫下來。

.otherwise({ 
      redirectTo: '/here give your destination path(e.g-404.html...etc)' 
     }); 
+0

編輯我的問題......你知道我怎麼可以調整此代碼,[我在另一個答案發現(http://stackoverflow.com/questions/24862225/page-doesnt-redirect-to-the -specified-page-on-invalid-url),我的代碼? –

+0

使用$ routeParams並不那麼簡單。當URL中有一些錯誤時,不用說$ routrParams。 –