Search code examples
angularjslocationhashtag

how to prevent $location in controller changing hash


The site is build in angularjs but the visualization is coded seperately not in angularjs. When adding $location to the controller of the app, it changes the hash-part in the url from

app/#hashpart 

to

app/#/hashpart 

Is there a way to prevent this? The visualization is creating and interpreting the first version and when I call my_function (that builds my site and calls the visualization):

$scope.$on('$locationChangeSuccess', function(event) {
    $scope.my_function(location.hash.substr(2));
});

then the browser switches between both hash-versions for eternety.


Solution

  • Use html5mode in angular js. This removes # from url.

    You need to make changes at server that whenever any page is requested, always reply with index.html, otherwise 404 will be encountered. (search for url rewrite rules for your server)