火angularjs事件时,标签/关闭浏览器 [英] fire angularjs event when tab/browser is closed
问题描述
我想火JS code在angularjs控制器。
I would like to fire js code in angularjs controller.
我有这样的:
$scope.$on('$destroy', function () {
alert('page1');
});
这是工作正常,当我从它使用的控制器,但是当我关闭标签/浏览器它不工作的页面导航了。
This is working fine when I'm navigating away from the page which is using that controller but its not working when I'm closing tab/browser.
我是否需要使用一些其他的code火JS code当标签/浏览器是关闭?
Do I need to use some other code to fire js code when tab/browser is closing?
由于提前,Laziale
Thanks in advance, Laziale
推荐答案
从角文档:
$摧毁();
删除当前范围(及其所有的孩子)从
父范围。去除意味着到$电话消化()将无
再传播到目前的范围及其子女。同时去除
表明当前的范围是符合垃圾收集。
Removes the current scope (and all of its children) from the parent scope. Removal implies that calls to $digest() will no longer propagate to the current scope and its children. Removal also implies that the current scope is eligible for garbage collection.
该$ destroy()方法通常使用的指令,如ngRepeat为
管理环的展开
The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.
一个范围被破坏就在一个$破坏事件上播出
这个范围。应用code可以注册一个$破坏事件处理程序
这将使它来执行任何必要的清理工作的机会。
Just before a scope is destroyed, a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it a chance to perform any necessary cleanup.
请注意的是,在AngularJS,也有一个$破坏jQuery的事件,该事件
可用于清理DOM绑定之前的元素被从取出
DOM的。
Note that, in AngularJS, there is also a $destroy jQuery event, which can be used to clean up DOM bindings before an element is removed from the DOM.
这只是处理这样的范围本身被破坏的情况下,似乎没有发生时,标签/浏览器,虽然关闭。
This just handles the situation where the scope itself is being destroyed, that does not seem to occur when the tab/browser is closed though.
您将不得不使用较低的水平onunload的方法和onbeforeunload事件来处理这种情况,这里是你可能会发现有用的另一篇文章:
You will have to use lower level methods onunload and onbeforeunload events to handle this situation, here is another post you may find useful:
<一个href=\"http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser\">javascript检测浏览器关闭标签/关闭浏览器
这篇关于火angularjs事件时,标签/关闭浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!