2017-05-19 71 views
1

我有一個模式與覆蓋當它打開。我在疊加層上有一個關閉模式的事件,但即使我點擊了模態本身,它也會關閉。 即時通訊嘗試此,只有當疊加被點擊時才關閉。關閉模式時,點擊覆蓋,而不是模式它自我

if (event.target.classList.contains('do-not-click-here')) 

但我得到這個錯誤。

Property 'classList' does not exist on type 'EventTarget' 

回答

0

我剛剛有這個問題。我在模型的容器上使用了一個模板參考,然後檢查它是否在event.path中,只要點擊疊加層上的點擊。

模板

<div class="overlay" (click)="overlayClicked($event)"> 
     <div class="modal" #modal> 
     <ng-content></ng-content> 
     </div> 
    </div> 

組件

export class ModalComponent implements OnInit { 
     @ViewChild('modal') modal: ElementRef; 
     visible = false; 

     constructor() { } 

     ngOnInit() { 
     } 

     overlayClicked(event) { 
     if(event.path.indexOf(this.modal.nativeElement) === -1){ 
      this.visible = false; 
     } 
     } 
    } 
+0

驚人!非常感謝。 –