Angular2中Bootstrap介面庫ng-bootstrap詳解_AngularJS

來源:互聯網
上載者:User

準備 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 , 相信已經由很多人蠢蠢欲動了吧!以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.