当 tab/browser 关闭时触发 Angularjs 事件
Fire Angularjs event when tab/browser is closed
我想在 Angularjs 控制器中触发 JS 代码。
我有这个:
$scope.$on('$destroy', function () {
alert('page1');
});
当我离开使用该控制器的页面时,它工作正常,但当我关闭时它不工作 tab/browser。
tab/browser 关闭时是否需要使用其他代码来触发 JS 代码?
来自 angular 文档:
$destroy();
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.
The $destroy() is usually used by directives such as ngRepeat for
managing the unrolling of the loop.
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.
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.
这只是处理作用域本身被破坏的情况,虽然 tab/browser 关闭时似乎不会发生这种情况。
您将不得不使用较低级别的 onunload 和 onbeforeunload 事件方法来处理这种情况,这是另一个您可能会觉得有用的 post:
javascript detect browser close tab/close browser
我想在 Angularjs 控制器中触发 JS 代码。
我有这个:
$scope.$on('$destroy', function () {
alert('page1');
});
当我离开使用该控制器的页面时,它工作正常,但当我关闭时它不工作 tab/browser。
tab/browser 关闭时是否需要使用其他代码来触发 JS 代码?
来自 angular 文档:
$destroy();
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.
The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.
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.
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.
这只是处理作用域本身被破坏的情况,虽然 tab/browser 关闭时似乎不会发生这种情况。
您将不得不使用较低级别的 onunload 和 onbeforeunload 事件方法来处理这种情况,这是另一个您可能会觉得有用的 post:
javascript detect browser close tab/close browser