在AngularJS的路线window.onbeforeunload的等价 [英] Equivalence of window.onbeforeunload in AngularJS for routes
问题描述
常规JavaScript有window.onbeforeunload。我如何模拟相同的AngularJS HTML5的路线?
Regular JavaScript has window.onbeforeunload. How do I emulate the same in AngularJS with HTML5 routes?
有为$ beforeRouteChange,但它不会让你,据我可以告诉取消事件。
There is $beforeRouteChange, but it doesn't allow you to cancel the event as far as I can tell.
要澄清:window.onbeforeunload将工作从页面导航离开,但不为帧内页面导航,例如从一个控制器到另一个刚刚通过HTML5历史API。
To clarify: window.onbeforeunload would work for navigating away from the page, but not for intra page navigation, e.g. going from one controller to another just through the HTML5 history API.
推荐答案
有哪些是可以取消一个 $ locationChangeStart
事件。
There is a $locationChangeStart
event which can be canceled.
由于尚未记载,这里是描述单元测试是如何影响 $路线
服务。
<一href=\"https://github.com/angular/angular.js/blob/v1.0.1/test/ng/routeSpec.js#L63\">https://github.com/angular/angular.js/blob/v1.0.1/test/ng/routeSpec.js#L63
Since it hasn't been documented yet, here is the unit test describing how it affects the $route
service.
https://github.com/angular/angular.js/blob/v1.0.1/test/ng/routeSpec.js#L63
此不提供像用户window.onbeforeunload
做了确认。你需要把你自己的对话框,并重新启动位置变化过程中获得相同的效果(或者你可以只使用同步 $ window.confirm
)。
This doesn't provide a confirmation for the user like window.onbeforeunload
does. You need to bring your own dialog service and restart the location change process to get the same effect (or you could just use the synchronous $window.confirm
).
这篇关于在AngularJS的路线window.onbeforeunload的等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!