2017-06-11 78 views
0

我正在尋找最佳的方法來實現模塊在Angular 4中遵循組件架構。Modals - Angular4

我見過很多教程,外部庫等,它的使用非常有限。基本上所有這些都遵循創建模態服務的方法,管理模式和模式組件,並通過transclude來根據內容更改模板。這要求我們需要將模態放入當前組件的模板中。事情是這樣的:

@Component({ 
selector: 'modal', 
template: ` 
    <div [ngClass]="{'closed': !isOpen}"> 
     <div class="modal-overlay" (click)="close(true)"></div> 

     <div class="modal"> 
      <div class="title" *ngIf="modalTitle"> 
       <span class="title-text">{{ modalTitle }}</span> 
       <span class="right-align" (click)="close(true)"><i class="material-icons md-24">clear</i></span> 
      </div> 

      <div class="body"> 
       <ng-content></ng-content> 
      </div> 
     </div> 
    </div> 

再使用這樣的:

<modal title='My title'> 
    <div> 
    My Modal 
    </div> 
</modal>` 

這對於小型應用罰款的做法。對於大型應用來說,這不是一個好的解決方案這個想法將是一個服務,打開組件動態而不定義它們。在這個組件中是這樣的:this.openModal(CustomModal)

ng-bootstrap做了一些與此類似的事情,但它使用的是Bootstrap 4,它仍然在alpha中,所以目前我們不確定它是否是一個有效的選項。任何以這種方式工作的圖書館或如何完成的任何方向?

回答

0

我強烈建議不要使用ngx-bootstrap,並使用@ng-bootstrap。 ngx-bootstrap缺乏功能,@ ng-bootstrap具有更多功能和穩定性。

我從ngx-bootstrap開始,最後改變它。我還看到Angular團隊在ng-conf中推薦了@ ng-bootstrap軟件包。

實際上,我用ngx-bootstrap的某個組件貢獻未完成。

+0

ng-bootstrap不需要Bootstrap 4?你能改變模態的基礎模板嗎? – Nocktu

+0

您可以使用@ ng-bootstrap和Bootstrap 3或4,都支持。你可以使用ng-template像ngx-bootstrap打開一個模式,並打開一個組件(這是我最喜歡的選項)'this.modalService.open(MyComponent)'這裏更多信息:https://ng-bootstrap.github.io/ #/ components/modal – Serginho

+0

是的,ng-bootstrap它可能是最有趣的,並且是我期待的。服務通過組件打開模式。根據您對圖書館的使用經驗,輸入和輸出對此有用嗎?其中一個想法是將對象項傳遞給模式,使用表單操作它,然後在請求後刷新主頁。所以基本上,模態需要向打開模式的組件發送響應。如果這是有道理的。 – Nocktu