【发布时间】:2013-03-06 13:02:28
【问题描述】:
我正在像这样使用 Angular 的 scrollTo 和 anchorScroll:
app.controller('TestCtrl', function($scope, $location, $anchorScroll) {
$scope.scrollTo = function(id) {
$location.hash(id);
$anchorScroll();
}
});
<a ng-click="scrollTo('foo')">Foo</a>
<div id="foo">Here you are</div>
我的问题是,当我单击链接时,页面会向下滚动,但在 50% 的情况下,页面会重新加载,因为 URL 中的哈希值发生了变化。
如何防止 Angular 重新加载页面?
更新: 我在这里找到了
https://groups.google.com/forum/?fromgroups=#!msg/angular/BY2ekZLbnIM/MORF-z2vHnIJ
那个
The $location service broadcasts a $locationChangeStart event. You can observe that and call event.preventDefault() to stop the navigation. Nice!
任何人都可以告诉如何观察该事件并防止默认
【问题讨论】:
标签: javascript angularjs