简体   繁体   English

Angular Route重定向到404

[英]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 : 这是我的问题:出于某种原因,在有效链接上,Angular无法找到我正在寻找的内容并返回404.这是路由配置:

  $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). 例如:指向/ menu / 45122245的链接(例如<a href="#/menu/{[{menu.id}]}" translate >SEE</a> )将在我打开时工作/菜单/视图,但不在/ home /(并返回404)。

Same URL is used, same object with same ID, so I don't know what is going on . 使用相同的URL,相同的ID具有相同的对象,所以我不知道发生了什么 I don't know if any other code could help you, let me know what you need :) 我不知道是否有其他代码可以帮助你,让我知道你需要什么:)

May be I am too late to reply to this post, but nevertheless I believe the following solution should do the trick, 可能是我来不及回复这篇文章,但我相信下面的解决方案应该可以解决问题,

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(). Angular要求您在主html文件()的头部指定url base,除非在传递给$ locationProvider.html5Mode()的html5Mode定义对象中将html5Mode.requireBase设置为false。 With that, relative urls will always be resolved to this base url, even if the initial url of the document was different. 这样,即使文档的初始URL不同,相对URL也将始终解析为此基本URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么角度重定向到路线但加载错误的视图 - Why angular redirects to the route but loads the wrong view 角度路线导致404-无法获取 - Angular route results in 404 - cannot GET Angular Controller在路线参数上返回404 - Angular Controller returns 404 on a Route Parameter <a href=“#”>到404页的</a>角度拦截器路线 - angular interceptor route <a href=“#”> to 404 page 直接访问子文件夹中的Angular 9路由会抛出404? - Accessing Angular 9 route in subfolder directly throws 404? 当路由参数和地址无效时,Angular 2不会重定向到404页面 - Angular 2 is not redirecting to 404 page when route param and address are invalid 如何以角度获取 404 错误页面上的先前路线? - How to get previous route on 404 Error page in angular? 刷新Angular RouteProvider路由会导致Python出现404错误 - Refreshing Angular RouteProvider route causes a 404 error with Python GitHub 页面不允许使用 Angular 自动重定向,它显示 404 GitHub 页面 - GitHub Pages doesn't allow automatic redirects with Angular, it shows 404 GitHub Page 在所有 ios 设备和 safari 浏览器中,在新选项卡中打开重定向到基础 url,不重定向到预期路线(角度) - In all ios devices and safari browser, open in new tab redirects to base url, not redirecting to expected route (Angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM