Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent $anchorScroll from modifying the url

Tags:

url

angularjs

I am using $anchorScroll to scroll to page top where the html element has ID #brand.

<body>
    <header id="brand">
        <!--Content-->
    </header>
</body>

AngularJS code:

$location.hash("brand");
$anchorScroll();

However, after $anchorScroll runs, the page url becomes http://localhost:8080/##brand, which means ##brand is appended to the original url. How can I keep the original url when using $anchorScroll? Thanks in advance!

like image 424
6324 Avatar asked Jun 28 '16 14:06

6324


2 Answers

Using $anchorScroll in its explicit form seems to work, at least when html5mode is on.

(do not call $location.hash())
$anchorScroll('brand');
like image 70
Brian Avatar answered Oct 22 '22 15:10

Brian


There's no way to prevent the hash from being added to the URL; you are explicitly doing just that by invoking $location.hash("brand");. You can, however, remove it by invoking $location.hash(null); after anchorScroll.

like image 1
Daniel Cottone Avatar answered Oct 22 '22 15:10

Daniel Cottone