Ngx-Bootstrap - 模態框



ngx-bootstrap 模態框元件是一個靈活且高度可配置的對話方塊提示,提供多種預設設定,並可以使用最少的程式碼。

ModalDirective

選擇器

  • [bsModal]

輸入

  • config − ModalOptions,允許透過元素屬性設定模態框配置

輸出

  • onHidden − 當模態框已完成從使用者視野中隱藏時觸發此事件(將等待 CSS 過渡完成)。

  • onHide − 呼叫 hide 例項方法時立即觸發此事件。

  • onShow − 呼叫 show 例項方法時立即觸發此事件。

  • onShown − 當模態框已對使用者可見時觸發此事件(將等待 CSS 過渡完成)。

方法

  • show() − 允許手動開啟模態框。

  • hide() − 允許手動關閉模態框。

  • toggle() − 允許手動切換模態框可見性。

  • showElement() − 顯示對話方塊。

  • focusOtherModal() − 事件技巧。

示例

由於我們將使用模態框,我們需要更新在ngx-bootstrap 下拉選單章節中使用的 app.module.ts 檔案,以使用ModalModuleBsModalService

更新 app.module.ts 以使用 ModalModule 和 BsModalService。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert';
import { ButtonsModule } from 'ngx-bootstrap/buttons';
import { FormsModule } from '@angular/forms';
import { CarouselModule } from 'ngx-bootstrap/carousel';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
import { BsDropdownModule,BsDropdownConfig } from 'ngx-bootstrap/dropdown';
import { ModalModule, BsModalService } from 'ngx-bootstrap/modal';

@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule,
      ButtonsModule,
      FormsModule,
      CarouselModule,
      CollapseModule,
      BsDatepickerModule.forRoot(),
      BsDropdownModule,
      ModalModule
   ],
   providers: [AlertConfig, BsDatepickerConfig, BsDropdownConfig,BsModalService],
   bootstrap: [AppComponent]
})
export class AppModule { }

更新 test.component.html 以使用模態框。

test.component.html

<button type="button" class="btn btn-primary" (click)="openModal(template)">Open modal</button>

<ng-template #template>
   <div class="modal-header">
      <h4 class="modal-title pull-left">Modal</h4>
      <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
         <span aria-hidden="true">×</span>
      </button>
   </div>
   <div class="modal-body">
      This is a sample modal dialog box.
   </div>
   <div class="modal-footer">
      <button type="button" class="btn btn-default" (click)="modalRef.hide()">Close</button>
   </div>
</ng-template>

更新 test.component.ts 以對應變數和方法。

test.component.ts

import { Component, OnInit, TemplateRef } from '@angular/core';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';

@Component({
   selector: 'app-test',
   templateUrl: './test.component.html',
   styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

   modalRef: BsModalRef;
   constructor(private modalService: BsModalService) {}

   openModal(template: TemplateRef<any>) {
      this.modalRef = this.modalService.show(template);
   }

   ngOnInit(): void {
   }
}

構建和執行

執行以下命令啟動 Angular 伺服器。

ng serve

伺服器啟動並執行後,開啟 https://:4200。點選“開啟模態框”按鈕並驗證以下輸出。

Modals
廣告
© . All rights reserved.