Angular2中Bootstrap界面庫ng-bootstrap詳解
準(zhǔn)備 Angular 2 環(huán)境
ng-bootstrap 是基于 Angular 2 的, 因此需要先準(zhǔn)備 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)進(jìn)來:
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css"/>
修改 app.component.ts
還需要修改一下 app.component.ts 文件, 導(dǎo)入 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>
顯示效果如下:
再來一個稍微復(fù)雜一點(diǎn)兒的, 在 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; }
在對應(yīng)的 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é)
實(shí)現(xiàn) ng-bootstrap 的人還是原來做 angular-ui 的那些人, 可以說配方還是原來的配方, 但是這味道么就跟原來有很大的不同了, 完全切換到了 Angular2 的風(fēng)格。
不過總的來說, ng-bootstrap 的推出將會極大的推進(jìn) Angular 2 在實(shí)際項(xiàng)目中的應(yīng)用, 而不只是停留在 demo 階段, 因?yàn)?AngularJS 1.x 時期, 很多項(xiàng)目都是以 AngularJS + UI-Bootstrap 為基礎(chǔ)的, 現(xiàn)在有了 Angular 2 的 ng-bootstrap , 相信已經(jīng)由很多人蠢蠢欲動了吧!以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
AngularJS控制器之間的數(shù)據(jù)共享及通信詳解
本文詳細(xì)介紹了AngularJS控制器之間的數(shù)據(jù)共享與通信,對angularjs共享數(shù)據(jù)及通信相關(guān)知識感興趣的朋友可以一起學(xué)習(xí)。2016-08-08Angular2中select用法之設(shè)置默認(rèn)值與事件詳解
在Angular.JS中可以使用數(shù)組或?qū)ο髣?chuàng)建一個下拉列表選項(xiàng)。關(guān)于Angular.js中select的基礎(chǔ)相信大家應(yīng)該都已經(jīng)了解了,那么下面這篇文章主要給大家介紹了Angular2中select用法之設(shè)置默認(rèn)值與事件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05angular8.5集成TinyMce5的使用和詳細(xì)配置(推薦)
這篇文章主要介紹了angular8.5集成TinyMce5的使用和詳細(xì)配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11理解AngularJs篇:30分鐘快速掌握AngularJs
這篇文章主要介紹了理解AngularJs篇:30分鐘快速掌握AngularJs,詳細(xì)介紹了AngularJs所涉及的知識點(diǎn),有興趣的可以了解一下。2016-12-12AngularJS ng-bind-html 指令詳解及實(shí)例代碼
本文主要是對AngularJS ng-bind-html 指令知識的詳細(xì)講解,并附代碼實(shí)例,有需要的小伙伴可以參考下2016-07-07AngularJS實(shí)現(xiàn)表單驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)表單驗(yàn)證功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01詳解AngularJS中module模塊的導(dǎo)入導(dǎo)出
本文給大家介紹angularjs中module模塊的導(dǎo)入導(dǎo)出,涉及到angularjs module相關(guān)知識,對angularjs module感興趣的朋友一起看看吧2015-12-12詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案
這篇文章主要介紹了詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07