如何停止角度重新加载时,地址变更 [英] How to stop Angular to reload when address changes
问题描述
我采用了棱角分明的 scrollTo
和 anchorScroll
是这样的:
I am using Angular's scrollTo
and anchorScroll
like this:
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中的散列变化。
My problem is that when i click the link the page scrolls down, but in 50% of cases the page reloads because the hash changes in the URL.
我如何prevent从重新加载页面角?
How can I prevent Angular from reloading the page?
更新:
我发现,在这里
Update: I have found that here
<一个href=\"https://groups.google.com/forum/?fromgroups=#!msg/angular/BY2ekZLbnIM/MORF-z2vHnIJ\">https://groups.google.com/forum/?fromgroups=#!msg/angular/BY2ekZLbnIM/MORF-z2vHnIJ
这
的$位置服务广播$ locationChangeStart事件。可以观察到,并调用事件。preventDefault()停止导航。太好了!
任何人都可以告诉你怎么观察事件和prevent默认
can anyone tell how to observe that event and prevent default
推荐答案
刷新正在发生的事情,因为没有对locationChangeStart事件的调用。您可以通过执行停止此:
The refresh is happening because there is a call to the locationChangeStart event. You can stop this by doing:
scope.$on('$locationChangeStart', function(ev) {
ev.preventDefault();
});
其实我最终编写使用它这里面叫我自己scrollTo指令。
I actually ended up writing my own scrollTo directive that uses this call inside of it.
<一个href=\"http://stackoverflow.com/questions/14712223/how-to-handle-anchor-hash-linking-in-angularjs/17928780#17928780\">My其他职位这个这里
这篇关于如何停止角度重新加载时,地址变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!