Ngx-Bootstrap - 工具提示



ngx-bootstrap 工具提示元件提供了一個易於使用且高度可配置的工具提示元件。

TooltipDirective

選擇器

  • [tooltip], [tooltipHtml]

輸入

  • adaptivePosition − 布林值,設定停用自適應位置。

  • container − 字串,指定應將工具提示附加到的元素的選擇器。

  • containerClass − 字串,工具提示容器的 CSS 類。

  • delay − 數字,顯示工具提示之前的延遲。

  • isDisabled − 布林值,允許停用工具提示。

  • isOpen − 布林值,返回工具提示當前是否正在顯示。

  • placement − 字串,工具提示的位置。接受:“top”、“bottom”、“left”、“right”。

  • tooltip − 字串 | TemplateRef<any>,要顯示為工具提示的內容。

  • tooltipAnimation − 布林值,預設:true。

  • tooltipAppendToBody − 布林值。

  • tooltipClass − 字串。

  • tooltipContext − any。

  • tooltipEnable − 布林值。

  • tooltipFadeDuration − 數字,預設:150。

  • tooltipHtml − 字串 | TemplateRef<any>。

  • tooltipIsOpen − 布林值。

  • tooltipPlacement − 字串

  • tooltipPopupDelay − 數字

  • tooltipTrigger − 字串 | 字串陣列

  • triggers − 字串,指定應觸發的事件。支援以空格分隔的事件名稱列表。

輸出

  • onHidden − 當工具提示隱藏時發出事件。

  • onShown − 當工具提示顯示時發出事件。

  • tooltipChange − 工具提示內容更改時觸發。

  • tooltipStateChanged − 工具提示狀態更改時觸發。

示例

由於我們將使用工具提示,因此我們必須更新在ngx-bootstrap 時間選擇器章節中使用的 app.module.ts 以使用TooltipModule

更新 app.module.ts 以使用 TooltipModule。

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 { PaginationModule,PaginationConfig } from 'ngx-bootstrap/pagination';
import { PopoverModule, PopoverConfig } from 'ngx-bootstrap/popover';
import { ProgressbarModule,ProgressbarConfig } from 'ngx-bootstrap/progressbar';
import { RatingModule, RatingConfig } from 'ngx-bootstrap/rating';
import { SortableModule, DraggableItemService } from 'ngx-bootstrap/sortable';
import { TabsModule, TabsetConfig } from 'ngx-bootstrap/tabs';
import { TimepickerModule } from 'ngx-bootstrap/timepicker';
import { TooltipModule  } from 'ngx-bootstrap/tooltip';

@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule,
      ButtonsModule,
      FormsModule,
      CarouselModule,
      CollapseModule,
      BsDatepickerModule.forRoot(),
      BsDropdownModule,
      ModalModule,
      PaginationModule,
      PopoverModule,
      ProgressbarModule,
      RatingModule,
      SortableModule,
      TabsModule,
      TimepickerModule.forRoot(),
      TooltipModule.forRoot()
   ],
   providers: [AlertConfig, 
      BsDatepickerConfig, 
      BsDropdownConfig,
      BsModalService,
      PaginationConfig,
      ProgressbarConfig,
      RatingConfig,
      DraggableItemService,
      TabsetConfig],
   bootstrap: [AppComponent]
})
export class AppModule { }

更新 test.component.html 以使用時間選擇器元件。

test.component.html

<timepicker [(ngModel)]="time"></timepicker>
<pre class="alert alert-info">Time is: {{time}}</pre>

<timepicker [(ngModel)]="time" [showMeridian]="false"></timepicker>
<pre class="alert alert-info">Time is: {{time}}</pre>

更新 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 {
   time: Date = new Date();
   constructor() {}
   ngOnInit(): void {
   } 
}

構建和執行

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

ng serve

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

Tooltip
廣告