Created
March 10, 2014 20:47
-
-
Save soyuka/9473982 to your computer and use it in GitHub Desktop.
Angular js keeping the hash anchor navigation with html5mode router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.config(function($stateProvider, $urlRouterProvider, $locationProvider) { | |
$locationProvider.hashPrefix('!').html5Mode(true) | |
}) | |
app.run(function($rootScope, $location) { | |
$rootScope.$on('$locationChangeStart', function(e, next, current) { | |
//prevent first load | |
if(next !== current) { | |
//create a dummy element ... | |
var nextElement = document.createElement('a'), currentElement = document.createElement('a') | |
nextElement.href = next | |
currentElement.href = current | |
//... to get the hash part and set it the way we want (by keeping the current path) | |
if(nextElement.hash.length !== 0) { | |
$location.path(currentElement.pathname) | |
$location.hash(nextElement.hash.substring(1)) | |
} | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment