2017-08-10 21 views
0

我跟着angular4本教程彈出模式:如果用戶點擊彈出一側,Angular4彈出即將關閉?

http://jasonwatmore.com/post/2017/01/24/angular-2-custom-modal-window-dialog-box

如果我點擊的模式之外,模態消失。但是我不希望模式消失,直到用戶單擊關閉圖標。

這裏是模態的OnInit

ngOnInit(): void { 
     let modal = this; 

     // ensure id attribute exists 
     if (!this.id) { 
      console.error('modal must have an id'); 
      return; 
     } 

     // move element to bottom of page (just before </body>) so it can be displayed above everything else 
     this.element.appendTo('body'); 

     // close modal on background click 
     this.element.on('click', function (e: any) { 
      var target = $(e.target); 
      if (!target.closest('.modal-body').length) { 
       modal.close(); 
      } 
     }); 

     // add self (this modal instance) to the modal service so it's accessible from controllers 
     this.modalService.add(this); 
    } 

這裏是我有什麼實際的plunker: https://plnkr.co/edit/gPCTvV?p=preview

+2

使用jQuery內部角度這樣的事情是不是一個很好的做法,我不建議這個教程:/ – Supamiu

+0

Supamiu我是新來的角,你能告訴我爲什麼在角度內使用jquey不是一個好的做法嗎? – Rahiman

+0

因爲JQuery做了什麼角度已經可以做到了,並且以一種更好的方式。 – Supamiu

回答

0

剛從app/_directives/modal.component.ts

// close modal on background click 
    this.element.on('click', function (e: any) { 
     var target = $(e.target); 
     if (!target.closest('.modal-body').length) { 
      modal.close(); 
     } 
    }); 

更新Plunker LINK

刪除此

更新

您可以利用材料對話或利用引導模態。

對於材質對話現場實例檢查此工作link並點擊聯繫人。你

還可以檢查此angular materail link

你的目的,你不希望外界點擊時,看看這個plunker被關閉的模式。

使用引導模式檢查這個answer它有一個工作演示過

+0

謝謝,你真的節省我的時間 – Rahiman

+0

有沒有更好的解決方案,而不使用jquery – Rahiman

+0

不,我不想使用使用材料,我使用bootstrap3 + angular4 – Rahiman