【问题标题】:Window Onfocus Process in AngularAngular中的窗口聚焦过程
【发布时间】:2018-06-14 15:05:38
【问题描述】:

我是 Angular v5 的新手,想知道下面的等价物是什么,特别是 $window.onfocus,因为这在 Angular 中不起作用

$window.onfocus = function() {     
   console.log("****user attempted leaving but changed its mind, do actions here");
}   

我需要在我的 TypeScript 代码中确定窗口焦点。

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    使用@HostListener装饰器在你的组件中注册window.focus事件

    然后

    @HostListener('window:focus', ['$event'])
    onFocus(event) {
        console.log("****user attempted leaving but changed its mind, do actions here");
    }
    

    工作DEMO

    您可以尝试在浏览器选项卡中运行演示 url

    【讨论】:

    • 嗨,阿米特 - 抱歉,我不明白您的演示以及它与您的答案的关系。你能解释一下吗?
    • 演示链接中有我的答案中显示的代码,它也打开了默认运行窗口。如果您想查看控制台日志,您必须在主浏览器中运行它并在窗口选项卡之间切换。这样您才能看到日志。答案中的 URL 是运行窗口的直接链接。
    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2010-10-01
    相关资源
    最近更新 更多