Angular.js: Anchor links in HTML5 mode go to 'base url' and trigger a navigation event

Created on 23 Dec 2013  路  10Comments  路  Source: angular/angular.js

I have an app with $locationProvider.html5Mode(true), so no hashes in the URLs; I figured I could use anchors like they used to again, but unfortunately that doesn't seem to be the case.

I made a plunkr to try and highlight the problem:

http://plnkr.co/edit/Upc89hCMMVbAhJaV2XYd?p=preview

full-screen with visible path version: http://run.plnkr.co/IA9Gwp52MzFpE1O6/

Issue 1 is that an <a href="#anchor"> will link to http://host/#anchor, instead of http://host/currenPath/#anchor.

Issue 2 is that when I create an url <a href="path/#anchor"> and click it, a navigation event is triggered - highlighted in the plunkr by a page transition animation.

StackOverflow questions offer answers like this work around the limitation by calling $location.hash() and $anchorScroll, but those will also trigger a navigation event. The second solution there is to reset $location.hash to the old value - i.e. none - to avoid the navigation event from triggering, but that removes the option to link directly to anchors from other pages and removes bookmarkability.

I came across issue #648 which seems to outline the same issue (two years ago), but it seems it stopped working sometime since then.

tl;dr: I'd like to do <a href="#anchor"> that links to an anchor at the current route, but it links to the base URL and triggers a navigation event.

PRs plz! $location moderate more info confusing bug

Most helpful comment

@fwielstra You need to 1. set the <base href> without the trailing slash, 2. configure hashPrefix
(see http://plnkr.co/edit/tgin0gpQl1qwu9hVXBno?p=preview). Then it works.

I totally agree that this is not simple at all! I have a suggestion to make this better - when you set html5Mode to true, we should set the hashPrefix to some default value (probably !). We need to make sure we set it only if it has not been already overridden. Any other ideas?

Also, when clicking the first two links (#lorem, #ipsum) there is some url flickering going on (the url is set to the base path first) - that should not happen. I think that's a bug.

So action items:

  1. set hashPrefix if html5Mode
  2. fix the url flickering bug

All 10 comments

@fwielstra You need to 1. set the <base href> without the trailing slash, 2. configure hashPrefix
(see http://plnkr.co/edit/tgin0gpQl1qwu9hVXBno?p=preview). Then it works.

I totally agree that this is not simple at all! I have a suggestion to make this better - when you set html5Mode to true, we should set the hashPrefix to some default value (probably !). We need to make sure we set it only if it has not been already overridden. Any other ideas?

Also, when clicking the first two links (#lorem, #ipsum) there is some url flickering going on (the url is set to the base path first) - that should not happen. I think that's a bug.

So action items:

  1. set hashPrefix if html5Mode
  2. fix the url flickering bug

Apologies for the late reply, christmas intervened :p. Thanks for the reply and investigating.

It does seem to sort of work in the plunkr now, yeah, minus the URL flickering you noticed yourself - and in the preview (with visible url), the plunker ID is replaced completely by '/path'. In my local / real application, the base href was '/'; removing it breaks all relative urls, breaking the application. The hashPrefix didn't seem to change anything, and an will create a link that leads to the 'base' href / the domain name.

Any updates on this?

I had the same issue. I found that it's because I had <base href="/">. Changing it to <base href=""> fixes the problem. I tested on Chrome, Firefox, and Safari.

@vojtajina Thanks ! I had the same issue and you explanations made me double check my base href. Removing the front slash did it for me.

@vojtajina The plunker doesn't seem to work for me. I added a log statement to the router controller so you can see that the controller is being re-instantiated every time you click. Is this expected?

http://plnkr.co/edit/a36L65c3vo8bFS0Bfszl?p=preview

I think this boils down to a configuration issue. Also, I couldn't reproduce any flickering. See here http://plnkr.co/edit/rwct8raZ4xCpYfxImg9B?p=preview

@Narretz Did you see my plnkr? Controller is getting reinstantiated every click.

http://plnkr.co/edit/a36L65c3vo8bFS0Bfszl?p=preview

@bennlich strange, I thought my plunker didn't show the problem anymore. But it does now.

Was this page helpful?
0 / 5 - 0 ratings