javascript - 如何在 Angular JS 中检测浏览器刷新和关闭事件?

标签 javascript angularjs

您好,我已尝试检测以下事件,但有时在关闭选项卡或浏览器时不会触发该事件。

$window.onbeforeunload

最佳答案

如果您只想关闭浏览器做一些事情,并且您确实希望在刷新页面时可以使用以下代码:

   window.onbeforeunload = function () {
    if (performance.navigation.type == 1) {
        localStorageService.remove("authorizationData");
    }
}

关于javascript - 如何在 Angular JS 中检测浏览器刷新和关闭事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47831845/

相关文章:

javascript - AngularJS $http.get ('' 中缺少参数)

javascript - jQuery 函数无法正确加载 Bootstrap 页脚

javascript - s% 如何引用 Helmet 组件内的 title 属性?

javascript - <input type "multiple"> 中的条件 ="file"属性与 AngularJS

php - 数据库文本无法解析为 JSON

javascript - 与传递的参数进行 Angular 数据绑定(bind)

javascript - 在 RxJS 中实现 for-await-of 语句

javascript - 正则表达式:允许用 ; 分隔多封电子邮件(分号)AND 允许空白/空值

javascript - 在指令中使用scope.$watch时出错

angularjs - 在 AngularJS 中从没有隔离作用域的指令调用 Controller 函数