Angular中的窗口Onfocus过程 [英] Window Onfocus Process in Angular

查看:101
本文介绍了Angular中的窗口Onfocus过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular v5的新手,想知道以下内容的等效含义,尤其是 $ window.onfocus ,因为这在Angular中不起作用

I am new to Angular v5 and wanted to know what the equivalent is for the following, specifically the $window.onfocus as this doesn't work in Angular

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

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

I need to determine window focus within my TypeScript code.

推荐答案

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

Use @HostListener decorator to register window.focus event in your component

然后

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

工作演示

您可以尝试在浏览器标签中运行演示 url .

You can try running the demo url in your browser tab.

这篇关于Angular中的窗口Onfocus过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆