0

I use angular ui route, works fine - but in browsers that doesn't support html5mode, and it has to fall back to hashbang, links aren't working.

www.test.com/something < works

www.test.com/#/something < not working. (redirects to test.com)

not quite sure how to make the hashbanged links work ?

my desperate attempt:

if (window.history && window.history.pushState) {
  // HTML5 history API is available.
  $locationProvider.html5Mode({
    enabled: true,
});
} else {
    // hashbang mode.
    window.location.hash = '/'; 
    $locationProvider.html5Mode({
      enabled: true,
    });
  }

$urlRouterProvider.otherwise("/");

$stateProvider
  .state('statistics', {
      url: "/statistics/:id",
      templateUrl: '../path/statistics.html',
      controller: 'ResultCtrl'
    }
);
Anders Pedersen
  • 2,255
  • 4
  • 25
  • 49

2 Answers2

1

you just need to do this:

$locationProvider.html5Mode({
    enabled: true,
}).hashPrefix("#");
scyrizales
  • 134
  • 6
  • usually you would have a "!" in hashprefix to get the classic #! hashbang. having .hashprefix('#') just adds a second hashtag to the url -> www.something.com/##/view1 – Anders Pedersen Apr 28 '16 at 14:32
  • yes @AndersPedersen But he was asking for #/something so i believe he wants it to work with # – scyrizales Apr 28 '16 at 14:36
0

You have enabled html5mode. This mode basically means the site doesn't route with hashbang urls. I don't think you can support both.

Remove this code:

$locationProvider.html5Mode({
  enabled: true,
});

Update

https://docs.angularjs.org/guide/$location#html5-mode

According to the docs you don't need to test for push state support, it automatically falls back to hashbangs if its not supported.

Sam
  • 1,725
  • 1
  • 17
  • 28
  • if html5mode is not available (using older browser) it will automatically go back to hashbang mode from what i could read from docs. – Anders Pedersen Apr 28 '16 at 14:28
  • you updated the post with what i just told you ? :) the reason for the check was a desperate attempt to edit the URL without # if html5mode not supported. – Anders Pedersen Apr 28 '16 at 14:34
  • yeh i was updating it when u commented. if in your code somewhere you call $location.path('/somethingvalid') (in ie9) what path does it try to browse to? – Sam Apr 28 '16 at 14:39