If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'.

Now we have a modal component, we want to click 'Esc' key to close the modal.

  <au-modal
class="auth-modal"
*auModalOpenOnClick="[loginButton, signUpButton]"
[closeOnClickOutside]="true"
[closeOnEsc]="true"
[body]="newModelBody">
<!-- Modal body -->
</au-modal>

We set two input variables: 'closeOnEsc' for keyboard event. And 'closeOnClickOutside' to click event.

import {Component, Input, OnInit, TemplateRef} from '@angular/core';
import {AuModalService} from './au-modal.service';
import {EventManager} from '@angular/platform-browser'; @Component({
selector: 'au-modal',
templateUrl: './au-modal.component.html',
styleUrls: ['./au-modal.component.scss']
})
export class AuModalComponent implements OnInit { @Input() body: TemplateRef<any>;
@Input() closeOnClickOutside = true;
@Input() closeOnEsc = true; constructor(private auModelService: AuModalService,
private eventManage: EventManager) {
} ngOnInit() {
this.eventManage.addGlobalEventListener('window', 'keyup.esc', () => {
if (this.closeOnEsc) {
this.closeModal();
}
})
} onClick() {
if (this.closeOnClickOutside) {
this.closeModal();
}
} closeModal() {
this.auModelService.close();
} cancelCloseModal(evt: KeyboardEvent) {
evt.preventDefault();
evt.stopPropagation();
} }

[Angular] Angular Global Keyboard Handling With EventManager的更多相关文章

  1. [转]Global exception handling in Web API 2.1 and NLog

    本文转自:https://stackoverflow.com/questions/25865610/global-exception-handling-in-web-api-2-1-and-nlog ...

  2. [Angular] Observable.catch error handling in Angular

    import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...

  3. Angular - - angular.Module

    angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...

  4. Angular - - angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  5. Angular - - Angular数据类型判断

    angular.isArray 判断括号内的值是否为数组. 格式:angular.isArray(value); value: 被判断是否为数组的值. ------------------------ ...

  6. Angular - - angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson

    angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...

  7. Angular - - angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

  8. Angular - - angular.element

    angular.element 将DOM元素或者HTML字符串一包装成一个jQuery元素. 格式:angular.element(element); element:包装成jquery对象的html ...

  9. Angular - - angular.equals

    angular.equals 对比两个对象/值是否相等.支持值类型.正则表达式.数组和对象. 如果下列至少有一个是正确的,则将两个对象/值视为相等. 两个对象/值能通过===比较. 两个对象/值是同一 ...

随机推荐

  1. How to install Armbian on Orange Pi Plus 2e

    bian on Orange Pi Plus 2e How to install Armbian on Orange Pi Plus 2e Armbian on the microSD You jus ...

  2. 最短路 spfa, dijkstra, Floyd

    spfa #include <stdio.h> #include <queue> using namespace std; #define RANGE 101 #define ...

  3. 虚拟机中试用windows 8(视频)

    虚拟机中试用windows 8(视频) VM7装windows 8基本没戏,建议用正式版vmware8.0,还有Oracle的Virtualbox 也没问题http://www.virtualbox. ...

  4. arp---操作主机的arp缓冲区

    简介 arp命令用于操作主机的arp缓冲区,可以用来显示arp缓冲区中的所有条目.删除指定的条目或者添加静态的ip地址与MAC地址对应关系. 选项 -a<主机>:显示arp缓冲区的所有条目 ...

  5. logname---显示用户名称

    logname命令用来显示用户名称.

  6. Django项目之Web端电商网站的实战开发(三)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  7. Django模型三

    关联对象操作及多表查询 关联表的数据操作: 一对多: 正向:如果一个模型有外键字段,通过这个模型对外键进行操作叫做正向. 更新: 通过属性赋值 In [1]: from teacher.models ...

  8. Jenkins学习总结(2)——Jenkins+Maven进行Java项目持续集成

    最近配置了Jenkins服务器,记录下基本过程.(当然还遇到了若干小问题,兵来将挡水来土掩就是了) Jenkins安装 安装Tomcat 从Jenkins官网下载jenkins.war文件.官网地址: ...

  9. wpf app全局变量传参方法(代码片段 )

    清空某行绑定的行数据: int RowIndex = datagrid.SelectedIndex; _Table.Rows[RowIndex]["AVERAGE_PRICE"] ...

  10. 【剑指Offer学习】【面试题49:把字符串转换成整数】

    题目:实现一个函数stringToInt,实现把字符串转换成整数这个功能.不能使用atoi或者其它相似的库函数. 题目解析 这看起来是非常easy的题目,实现基本功能 ,大部分人都能用10行之内的代码 ...