1
私は、クリック時にサブメニューを開くディレクティブを持っていますが、ターゲット要素がクリックされたときにドキュメントをクリックするだけで改善したいと思います。ですから、このディレクティブを改善するか、ホストリスナーを動的に追加する方法です。角2は、ディレクティブに動的にホストリスナーを追加します
import { Directive, HostListener, Input, AfterContentInit, ElementRef, ViewContainerRef, ContentChild } from '@angular/core';
@Directive({
selector: '[appSubMenu]'
})
export class SubMenuDirective implements AfterContentInit {
private visible: boolean = false;
@ContentChild('subMenu') child: ElementRef;
constructor(private vcRef: ViewContainerRef) { }
ngAfterContentInit() {
this.child.nativeElement.style.display = 'none';
}
@HostListener('document:click', ['$event', '$event.target'])
show(event:MouseEvent, targetElem: HTMLElement): void {
if (this.vcRef.element.nativeElement === targetElem && !this.visible) {
this.child.nativeElement.style.display = 'block';
this.visible = true;
} else {
this.child.nativeElement.style.display = 'none';
this.visible = false;
}
}
}