I find it a bit strange that your plain href
doesn't scroll and ng-href
scrolls, I thought it was the other way round...
But, to the solution; It's up to the browser to scroll on hash changes, so usually to disable it you need to intercept and preventDefault()
the event and then change the location by yourself. When using Angular, you can either define some attribute directive for the <a>
element or just define your own a
directive.
If you use ng-view
, it relies on $anchorScroll
service with view updates to simulate the behaviour what the browser would normally do, as Angular already intercepts the event. You can prevent the scroll on view load by providing your own $anchorScroll
which does nothing:
angular.module('yourModule', []).value('$anchorScroll', angular.noop);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…