We have modal implement and now we want to implement close functionality.

Becuase we use a structure directive to do open modal on click functionality. So as you can see, the directive and modal component is spreated. We need to have some way to communcation between directive and component.

  <ng-template [auModalOpenOnClick]="[loginButton, signUpButton]">
<au-modal class="auth-modal" [body]="newModelBody">
<!-- modal body -->
</au-modal>
</ng-template>

One general communction mechanism is though serivce.

So we need to create a service which only for AuModal component:

import {NgModule, ModuleWithProviders} from '@angular/core';
import {AuModalComponent} from './au-modal.component';
import {CommonModule} from '@angular/common';
import { AuModalOpenOnClickDirective } from './au-modal-open-on-click.directive';
import {AuModalService} from 'app/au-modal/au-modal.service'; @NgModule({
declarations: [AuModalComponent, AuModalOpenOnClickDirective],
imports: [
CommonModule
],
exports: [
AuModalComponent,
AuModalOpenOnClickDirective
]
})
export class AuModalModule { /*
* Inject AuModuleService here instead of global providers is for lazy loading
* to prevent duplicate serivce name.
* */
static forRoot(): ModuleWithProviders {
return {
ngModule: AuModalModule,
providers: [
AuModalService
]
}
}
}

Because we don't want it to be global, it might affect lazy loading, have naming conflicts with other third party libs. Therefore we use 'forRoot' static method on the AuModalModule. This approach is good for lazy loading.

Service:

Service is Observable based implementation. It mean we gonna have one public method call 'close' and an public observable variable call 'close$'.

Once close() method was called, 'close$' can get notifiied.

import { Injectable } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject'; @Injectable()
export class AuModalService { private subject = new Subject();
close$: Observable<any> = this.subject.asObservable();
constructor() { } close() {
this.subject.next();
} }

AuModal component: We want user click outside modal body to close the modal, so we bind 'closeModal' function tot he overlay wrapper. And also we don't want user click modal body itself to trigger the close event also. So we have second method called 'cancelCloseModal' to stop event propagation.

<div class="modal-overlay" (click)="closeModal()">

  <div class="modal-body" (click)="cancelCloseModal($event)">

    <ng-container *ngIf="body; else projectionBody">
<ng-container *ngTemplateOutlet="body"></ng-container>
</ng-container> <ng-template #projectionBody>
<ng-content></ng-content>
</ng-template> </div> </div>
  closeModal() {
this.auModelService.close();
} cancelCloseModal(evt: KeyboardEvent) {
evt.preventDefault();
evt.stopPropagation();
}

Now the only thing leave to do is subscribe the 'close$' observable inside the directive, once event was triggered, we clear the component.

Directive:

import {Directive, Input, OnInit, TemplateRef, ViewContainerRef} from '@angular/core';
import {AuModalService} from './au-modal.service'; @Directive({
selector: '[auModalOpenOnClick]'
})
export class AuModalOpenOnClickDirective implements OnInit{
ngOnInit(): void {
this.auModalService.close$
.subscribe(() => this.viewContainer.clear());
} @Input()
set auModalOpenOnClick (els) { let elements: HTMLBaseElement[]; if(Array.isArray(els)) {
elements = els;
} else {
elements = [els];
} elements.forEach(el => {
el.addEventListener('click', () => {
this.viewContainer.clear();
this.viewContainer.createEmbeddedView(this.template);
});
});
} constructor(
private template: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private auModalService: AuModalService
) { } }

[Angular] Implementing A General Communication Mechanism For Directive Interaction的更多相关文章

  1. Linux Communication Mechanism Summarize

    目录 . Linux通信机制分类简介 . 控制机制 0x1: 竞态条件 0x2: 临界区 . Inter-Process Communication (IPC) mechanisms: 进程间通信机制 ...

  2. [转]A Faster UIWebView Communication Mechanism

    ref:http://blog.persistent.info/2013/10/a-faster-uiwebview-communication.html Use location.hash or t ...

  3. angular源码分析:$compile服务——directive他妈

    一.directive的注册 1.我们知道,我们可以通过类似下面的代码定义一个指令(directive). var myModule = angular.module(...); myModule.d ...

  4. [Angular] Implementing a ControlValueAccessor

    So when you need to create a ControlValueAccessor? When you want to use a custom component as form c ...

  5. NetLink Communication Mechanism And Netlink Sourcecode Analysis

    catalog . Netlink简介 . Netlink Function API Howto . Generic Netlink HOWTO kernel API . RFC Linux Netl ...

  6. Angular之指令Directive系列

    项目筹备近期开启Angular学习,指令比较难理解所以记录备案,推荐Angualr实战学习视频大漠穷秋 Angular实战 一.指令directive概述 指令可以对元素绑定事件监听或者改变DOM结构 ...

  7. ANGULARJS: UNDERSTANDING DIRECTIVE SCOPE

    https://www.3pillarglobal.com/insights/angularjs-understanding-directive-scope --------------------- ...

  8. Angular中Constructor 和 ngOnInit 的本质区别

    在Medium看到一篇Angular的文章,深入对比了 Constructor 和 ngOnInit 的不同,受益匪浅,于是搬过来让更多的前端小伙伴看到,翻译不得当之处还请斧正. 本文出处:The e ...

  9. PatentTips - Compare and exchange operation using sleep-wakeup mechanism

    BACKGROUND Typically, a multithreaded processor or a multi-processor system is capable of processing ...

随机推荐

  1. 类数组对象arguments 和 数组对象

    arguments并不是一个真正的数组,而是一个“类似数组(array-like)”的对象: 就像下面的这段输出,就是典型的类数组对象: {0:12, 1:23} 一.类数组 VS 数组 相同点: 都 ...

  2. Oracle 启动失败报错“TNS-12555: TNS:permission denied”解决办法

    [oracle@testdb admin]$ lsnrctl start   LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 10-FEB- ...

  3. js24---工厂模式2

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. 9.Maven之(九)依赖关系

    转自:https://yq.aliyun.com/ziliao/312160 在maven的管理体系中,各个项目组成了一个复杂的关系网,但是每个项目都是平等的,是个没有贵贱高低,众生平等的世界,全球每 ...

  5. MySQL轻量版使用,无需安装,无脑操作

    不知道是否有想我一样的,开始用的都是安装版的,特别费事,卸载后注册表很难删除 下面介绍一下MySQL轻量级的如下 首先打开一个网址:www.oracle.com没错就是强大的Oracle官网 也可以直 ...

  6. Android 基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器aaa

    MDPlayer万能播放器 MDPlayer,基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器,可以播 ...

  7. CentOS不能进入登录界面

    http://blog.csdn.net/powerzone/article/details/6798646

  8. git 当出现 devirge 时,一个是commit的提交顺序不对

    进入新分支1.git pull origin branchname 2.修改 3.git add . 4.git commit  5. git pull 6.git push 出现分歧要,就版本回退, ...

  9. linux网站发布操作流程

    Linux 添加用户命令: useradd bm -g webTemp http://www.runoob.com/linux/linux-vim.html Linux关于网站发布操作流程 虚拟机地下 ...

  10. 【组件】微信小程序input搜索框的实现

    开发小程序的过程,是一个学习知识,解决问题的过程,每当实现了一个需求,总会有很大的成就感,每天记录一个开发过程中的细节.实现效果如下: 官方参考链接:https://developers.weixin ...