Skip to content

Instantly share code, notes, and snippets.

@soyuka
Created March 10, 2014 20:47
Show Gist options
  • Save soyuka/9473982 to your computer and use it in GitHub Desktop.
Save soyuka/9473982 to your computer and use it in GitHub Desktop.
Angular js keeping the hash anchor navigation with html5mode router
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