Angular Route redirects to 404
Here's my problem : for some reason, on valid links, Angular can't find what I'm looking for and return the 404. Here's the route configuration :
$routeProvider.when('/', {templateUrl: 'partials/home.html'});
$routeProvider.when('/home', {templateUrl: 'partials/home.html'});
$routeProvider.when('/menus', {templateUrl: 'partials/menus.html'});
$routeProvider.when('/menu/:menuId', {templateUrl: 'partials/menu.html', controller: 'ShowMenuCtrl'});
$routeProvider.when('/products', {templateUrl: 'partials/products.html'});
$routeProvider.when('/product/:productId', {templateUrl: 'partials/product.html', controller: 'ShowProductCtrl'});
$routeProvider.when('/drink/:productId', {templateUrl: 'partials/product.html', controller: 'ShowProductCtrl'});
$routeProvider.when('/drinks', {templateUrl: 'partials/drinks.html'});
$routeProvider.when('/order', {templateUrl: 'partials/order.html'});
$routeProvider.when('/404', {templateUrl: 'partials/404.html'});
$routeProvider.otherwise({redirectTo: '/404'});
For example : a link ( such as <a href="#/menu/{[{menu.id}]}" translate >SEE</a>
) pointing to /menu/45122245, will work when I'm on /menus/ view, but not on /home/ (and return the 404).
Same URL is used, same object with same ID, so I don't know what is going on . I don't know if any other code could help you, let me know what you need :)
可能是我迟到回复这篇文章,但我相信下面的解决方案应该做的伎俩,
angular.module('moduleName')
..config(['$compileProvider', '$routeProvider', function ($compileProvider, $routeProvider) {
$routeProvider.when('/', {templateUrl: 'partials/home.html'});
$routeProvider.when('/home', {templateUrl: 'partials/home.html'});
$routeProvider.when('/menus', {templateUrl: 'partials/menus.html'});
$routeProvider.when('/menu/:menuId', {templateUrl: 'partials/menu.html', controller: 'ShowMenuCtrl'});
$routeProvider.when('/products', {templateUrl: 'partials/products.html'});
$routeProvider.when('/product/:productId', {templateUrl: 'partials/product.html', controller: 'ShowProductCtrl'});
$routeProvider.when('/drink/:productId', {templateUrl: 'partials/product.html', controller: 'ShowProductCtrl'});
$routeProvider.when('/drinks', {templateUrl: 'partials/drinks.html'});
$routeProvider.when('/order', {templateUrl: 'partials/order.html'});
$routeProvider.when('/404', {templateUrl: 'partials/404.html'});
$routeProvider.otherwise({redirectTo: '/404'});
}])
.run(['$location', function ($location) {
if ($location.path() === '' && $location.$$absUrl.indexOf('/partials') > -1) {
$location.path('/home')
}
}]);
Angular requires you to specify the url base in the head of your main html file () unless html5Mode.requireBase is set to false in the html5Mode definition object passed to $locationProvider.html5Mode(). With that, relative urls will always be resolved to this base url, even if the initial url of the document was different.
链接地址: http://www.djcxy.com/p/20636.html上一篇: 上传文件时Nginx 502 Bad Gateway
下一篇: Angular Route重定向到404