2015-05-09 117 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。 –