1
我有一個指令,可以在點擊時打開一個子菜單,但是我想通過僅在點擊目標元素時激活文檔點擊來改進它。所以問題是改進這個指令或者如何動態添加主機監聽器。Angular 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;
}
}
}