Ngx-Bootstrap - 警報



警報為典型的使用者操作(如資訊、錯誤)提供上下文訊息,並提供可用且靈活的警報訊息。

AlertComponent

顯示可摺疊的內容面板,用於在有限的空間內呈現資訊。

選擇器

  • alert, bs-alert

輸入

  • dismissible − 布林值,如果設定,則顯示內聯“關閉”按鈕,預設值:false

  • dismissOnTimeout − 字串 | 數字,警報關閉之前的毫秒數

  • isOpen − 布林值,警報是否可見,預設值:true

  • type − 字串,警報型別。提供四個 Bootstrap 支援的上下文類之一:success、info、warning 和 danger,預設值:warning

輸出

  • onClose − 此事件在呼叫 close 例項方法後立即觸發,$event 是 Alert 元件的例項。

  • onClosed − 此事件在警報關閉時觸發,$event 是 Alert 元件的例項

AlertConfig

屬性

  • dismissible − 布林值,警報是否預設可關閉,預設值:false

  • dismissOnTimeout − 數字,警報關閉之前的預設時間,預設值:undefined

  • type − 字串,預設警報型別,預設值:warning

示例

由於我們將使用警報,因此我們需要更新在ngx-bootstrap 手風琴章節中使用的 app.module.ts,以使用AlertModuleAlertConfig

更新 app.module.ts 以使用 AlertModule 和 AlertConfig。

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';
@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule
   ],
   providers: [AlertConfig],
   bootstrap: [AppComponent]
})
export class AppModule { }

更新 test.component.html 以使用警報。

test.component.html

<alert type="success" 
   [dismissible]="dismissible"
   [isOpen]="open"
   (onClosed)="log($event)"
   [dismissOnTimeout]="timeout">
   <h4 class="alert-heading">Well done!</h4>
   <p>Success Message</p>
</alert>
<alert type="info">
   <strong>Heads up!</strong> Info
</alert>
<alert type="warning">
   <strong>Warning!</strong> Warning
</alert>
<alert type="danger">
   <strong>Oh snap!</strong> Error
</alert>

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

test.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-test',
   templateUrl: './test.component.html',
   styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
   open: boolean = true;
   dismissible: boolean = true;
   timeout: number = 10000;
   constructor() { }
   
   ngOnInit(): void {
   }
   log(alert){
      console.log('alert message closed');
   }
}

構建和服務

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

ng serve

伺服器啟動並執行後,開啟 https://:4200 並驗證以下輸出。

alerts
廣告
© . All rights reserved.