-->

Prevent $anchorScroll from modifying the url

2020-08-20 07:42发布

问题:

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!

回答1:

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

(do not call $location.hash())
$anchorScroll('brand');


回答2:

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.



标签: angularjs url