準備 Angular 2 環境
ng-bootstrap 是基于 Angular 2 的, 因此需要先準備 Angular 2 的環境。
使用 ng-bootstrap
下載 ng-bootstrap
ng-bootstrap 使用 bootstrap 4.0 alpha2 , 因此需要先下載 bootstrap , 推薦使用 npm 包的形式:
npm install bootstrap@4.0.0-alpha.2 --save
接著下載 ng-bootstrap , 同樣使用 npm 包的形式:
npm install @ng-bootstrap/ng-bootstrap --save
修改 systemjs.config.js
現在需要修改一下 systemjs.config.js 文件, 讓 SystemJS 能夠正確加載 ng-bootstrap :
// map tells the System loader where to look for thingsvar map = { 'app': 'dist', // 'dist', '@angular': 'node_modules/@angular', 'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api', 'rxjs': 'node_modules/rxjs', // add ng-bootstrap location map '@ng-bootstrap': 'node_modules/@ng-bootstrap'};// packages tells the System loader how to load when no filename and/or no extensionvar packages = { 'app': { main: 'main.js', defaultExtension: 'js', format: 'amd' }, 'rxjs': { defaultExtension: 'js' }, 'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' }, // add ng-bootstrap package config '@ng-bootstrap/ng-bootstrap': { main: 'index.js', defaultExtension: 'js' }};修改 index.html
index.html 文件也要修改一下, 把 bootstrap 的樣式表關聯進來:
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css"/>
修改 app.component.ts
還需要修改一下 app.component.ts 文件, 導入 ng-bootstrap 的指令:
import { Component, OnInit } from '@angular/core';import { HTTP_PROVIDERS } from '@angular/http';import { provideRouter, ROUTER_DIRECTIVES } from '@angular/router';// import ng-bootstrap directivesimport { NGB_DIRECTIVES, NGB_PRECOMPILE } from '@ng-bootstrap/ng-bootstrap';import { routes } from './app.routes';@Component({ //moduleId: module.id, selector: 'app', providers: [ HTTP_PROVIDERS ], templateUrl: 'dist/app.component.html', styleUrls: ['dist/app.component.css'], // ng-bootstrap required precompile directives precompile: [NGB_PRECOMPILE], // add ng-bootstrap directives to app directives: [ ROUTER_DIRECTIVES, NGB_DIRECTIVES ], pipes: []})export class AppComponent implements OnInit { ngOnInit() { }}ng-bootstrap 以指令 (directive) 的形式提供組件, 方便在 html 視圖中使用, 選擇器 (selector) 使用同一的前綴 ngb , 類名則統一使用 Ngb 前綴。
接下來就可以使用 ng-bootstrap 的組件了, 接下來以 NgbAlert 為例說明 ng-bootstrap 的用法。
NgbAlert 的 selector 是 ngb-alert , 支持的 Input 有 dismissible 和 type , Output 有 close 。
接下來看一個 NgbAlert 的例子:
<p> <ngb-alert [dismissible]="false"> <strong>Warning!</strong> Better check yourself, you're not looking too good. </ngb-alert></p>
顯示效果如下:

再來一個稍微復雜一點兒的, 在 app.component.ts 文件中添加下面的代碼:
export class AppComponent implements OnInit { alert: IAlert[]; ngOnInit() { this.alert = [ { id: 1, type: 'success', message: 'This is an success alert', }, { id: 2, type: 'info', message: 'This is an info alert', }, { id: 3, type: 'warning', message: 'This is a warning alert', }, { id: 4, type: 'danger', message: 'This is a danger alert', } ]; } closeAlert(alert: IAlert) { const index: number = this.alerts.indexOf(alert); this.alerts.splice(index, 1); }}interface IAlert { id: number; type: string; message: string;}在對應的 html 文件中添加 *ngFor 指令, 綁定 alerts 數組:
<p *ngFor="let alert of alerts"> <ngb-alert [type]="alert.type" (close)="closeAlert(alert)">/{/{ alert.message }} </ngb-alert></p>現在得到的效果如下圖所示:

ng-bootstrap 還有更多的組件, 就不一一列舉了。
總結
實現 ng-bootstrap 的人還是原來做 angular-ui 的那些人, 可以說配方還是原來的配方, 但是這味道么就跟原來有很大的不同了, 完全切換到了 Angular2 的風格。
不過總的來說, ng-bootstrap 的推出將會極大的推進 Angular 2 在實際項目中的應用, 而不只是停留在 demo 階段, 因為 AngularJS 1.x 時期, 很多項目都是以 AngularJS + UI-Bootstrap 為基礎的, 現在有了 Angular 2 的 ng-bootstrap , 相信已經由很多人蠢蠢欲動了吧!以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
新聞熱點
疑難解答