博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Implementing A General Communication Mechanism For Directive Interaction
阅读量:5131 次
发布时间:2019-06-13

本文共 3594 字,大约阅读时间需要 11 分钟。

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. 

 

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
= 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.

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
, private viewContainer: ViewContainerRef, private auModalService: AuModalService ) { }}

 

转载于:https://www.cnblogs.com/Answer1215/p/7100393.html

你可能感兴趣的文章
《DSP using MATLAB》Problem 7.37
查看>>
ROS lesson 1
查看>>
js笔记
查看>>
c风格字符串函数
查看>>
python基础学习第二天
查看>>
java可重入锁reentrantlock
查看>>
浅谈卷积神经网络及matlab实现
查看>>
struts2学习(9)struts标签2(界面标签、其他标签)
查看>>
Android 导入jar包 so模块--导入放置的目录
查看>>
解决ajax请求cors跨域问题
查看>>
Android Studio
查看>>
zz 圣诞丨太阁所有的免费算法视频资料整理
查看>>
【大数模板】C++大数类 大数模板
查看>>
【123】
查看>>
《收获,不止Oracle》pdf
查看>>
用户权限设置
查看>>
java 之equals与"=="的区别
查看>>
LinkedList<E>源码分析
查看>>
学习微软 Excel 2002 VBA 编程和XML,ASP技术
查看>>
LeetCode - Combinations
查看>>