[Angular] Angular Global Keyboard Handling With EventManager
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的更多相关文章
- [转]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 ...
- [Angular] Observable.catch error handling in Angular
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...
- Angular - - angular.Module
angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...
- Angular - - angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- Angular - - Angular数据类型判断
angular.isArray 判断括号内的值是否为数组. 格式:angular.isArray(value); value: 被判断是否为数组的值. ------------------------ ...
- Angular - - angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson
angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...
- Angular - - angular.bind、angular.bootstrap、angular.copy
angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...
- Angular - - angular.element
angular.element 将DOM元素或者HTML字符串一包装成一个jQuery元素. 格式:angular.element(element); element:包装成jquery对象的html ...
- Angular - - angular.equals
angular.equals 对比两个对象/值是否相等.支持值类型.正则表达式.数组和对象. 如果下列至少有一个是正确的,则将两个对象/值视为相等. 两个对象/值能通过===比较. 两个对象/值是同一 ...
随机推荐
- JS --- 延迟加载的几种方式
标题:JS延迟加载,也就是等页面加载完成之后再加载 JavaScript 文件. JS延迟加载有助于提高页面加载速度. 1. defer 属性 HTML 4.01 为 <script>标 ...
- vue中computed与watch的异同
一.computed 和 watch 都可以观察页面的数据变化.当处理页面的数据变化时,我们有时候很容易滥用watch. 而通常更好的办法是使用computed属性,而不是命令是的watch回调. ...
- NewtonSoft对象转json时,把 NULL 转 "" , 过滤 NULL, DateTime 时间类型去除 T
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = &quo ...
- react-native React Native version mismatch
android/app/build.gradle file: dependencies { compile fileTree(dir: "libs", include: [ ...
- WPF MVVM架构 EF、WCF、IOC 设计示例经典
概要 该演示项目利用WPF应用程序构建的MVVM架构示例, 运用了Unity容器接口注入, MVVM的经典设计, 后台利用的EF+WCF. 后台实现: 从数据库生成的emdx 结合上下文进行数据交互, ...
- start_kernel----lcokdep_init
void lockdep_init(void) { int i; /* * Some architectures have their own start_kernel() * code which ...
- 求第k大的数(用到快速排序算法的思想)
//下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostr ...
- go reflect 调用方法
package main import ( "fmt" "reflect" ) type A struct { } func (A) Test() { fmt. ...
- 【arc062e】Building Cubes with AtCoDeer
Description STL有n块瓷砖,编号从1到n,并且将这个编号写在瓷砖的正中央: 瓷砖的四个角上分别有四种颜色(可能相等可能不相等),并且用Ci,0,Ci,1,Ci,2,Ci,3分别表示左上. ...
- var、let和const的区别
var 首先var有变量提升 console.log(a); // undefined var a = 1; function也存在提升现象 console.log(b); //function b( ...