Angular2中Bootstrap界面庫ng-bootstrap詳解
準備 Angular 2 環(huán)境
ng-bootstrap 是基于 Angular 2 的, 因此需要先準備 Angular 2 的環(huán)境。
使用 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
現(xiàn)在需要修改一下 systemjs.config.js 文件, 讓 SystemJS 能夠正確加載 ng-bootstrap :
// map tells the System loader where to look for things
var 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 extension
var 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 的樣式表關(guān)聯(lián)進來:
<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 directives
import { 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 , 類名則統(tǒng)一使用 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 數(shù)組:
<p *ngFor="let alert of alerts">
<ngb-alert
[type]="alert.type"
(close)="closeAlert(alert)">\{\{ alert.message }}
</ngb-alert>
</p>
現(xiàn)在得到的效果如下圖所示:

ng-bootstrap 還有更多的組件, 就不一一列舉了。
總結(jié)
實現(xiàn) ng-bootstrap 的人還是原來做 angular-ui 的那些人, 可以說配方還是原來的配方, 但是這味道么就跟原來有很大的不同了, 完全切換到了 Angular2 的風格。
不過總的來說, ng-bootstrap 的推出將會極大的推進 Angular 2 在實際項目中的應用, 而不只是停留在 demo 階段, 因為 AngularJS 1.x 時期, 很多項目都是以 AngularJS + UI-Bootstrap 為基礎(chǔ)的, 現(xiàn)在有了 Angular 2 的 ng-bootstrap , 相信已經(jīng)由很多人蠢蠢欲動了吧!以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
AngularJS控制器之間的數(shù)據(jù)共享及通信詳解
本文詳細介紹了AngularJS控制器之間的數(shù)據(jù)共享與通信,對angularjs共享數(shù)據(jù)及通信相關(guān)知識感興趣的朋友可以一起學習。2016-08-08
angular8.5集成TinyMce5的使用和詳細配置(推薦)
這篇文章主要介紹了angular8.5集成TinyMce5的使用和詳細配置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
理解AngularJs篇:30分鐘快速掌握AngularJs
這篇文章主要介紹了理解AngularJs篇:30分鐘快速掌握AngularJs,詳細介紹了AngularJs所涉及的知識點,有興趣的可以了解一下。2016-12-12
AngularJS ng-bind-html 指令詳解及實例代碼
本文主要是對AngularJS ng-bind-html 指令知識的詳細講解,并附代碼實例,有需要的小伙伴可以參考下2016-07-07
詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案
這篇文章主要介紹了詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

