Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 281
enguerranws Avatar asked May 20 '14 10:05

enguerranws


2 Answers

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')
    }
}]);
like image 199
MechanicalCoder Avatar answered Oct 14 '22 01:10

MechanicalCoder


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.

like image 28
Devendra Bhatte Avatar answered Oct 14 '22 01:10

Devendra Bhatte