欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular2中Bootstrap界面庫ng-bootstrap詳解

 更新時間:2016年10月18日 10:57:30   投稿:daisy  
不知道大家有沒有留意,最近angular-ui團隊終于正式發(fā)布了基于 Angular2的Bootstrap界面庫ng-bootstrap ,之前工作中一直在用 AngularJS 1.x 的UI Bootstrap , 因此對這個ng-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 的樣式表關聯(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 還有更多的組件, 就不一一列舉了。

總結

實現(xiàn) ng-bootstrap 的人還是原來做 angular-ui 的那些人, 可以說配方還是原來的配方, 但是這味道么就跟原來有很大的不同了, 完全切換到了 Angular2 的風格。

不過總的來說, ng-bootstrap 的推出將會極大的推進 Angular 2 在實際項目中的應用, 而不只是停留在 demo 階段, 因為 AngularJS 1.x 時期, 很多項目都是以 AngularJS + UI-Bootstrap 為基礎的, 現(xiàn)在有了 Angular 2 的 ng-bootstrap , 相信已經由很多人蠢蠢欲動了吧!以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關文章

  • 在Angular使用ng-container元素的操作詳解

    在Angular使用ng-container元素的操作詳解

    ng-container 是 Angular 2+ 中可用的一個元素,可以作為結構指令的宿主,在本文中,您將探討可以使用 ng-container 解決的場景,文中有相關的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下
    2024-02-02
  • AngularJS控制器之間的數(shù)據共享及通信詳解

    AngularJS控制器之間的數(shù)據共享及通信詳解

    本文詳細介紹了AngularJS控制器之間的數(shù)據共享與通信,對angularjs共享數(shù)據及通信相關知識感興趣的朋友可以一起學習。
    2016-08-08
  • Angular2中select用法之設置默認值與事件詳解

    Angular2中select用法之設置默認值與事件詳解

    在Angular.JS中可以使用數(shù)組或對象創(chuàng)建一個下拉列表選項。關于Angular.js中select的基礎相信大家應該都已經了解了,那么下面這篇文章主要給大家介紹了Angular2中select用法之設置默認值與事件的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • 詳解AngularJS Filter(過濾器)用法

    詳解AngularJS Filter(過濾器)用法

    這篇文章主要介紹了AngularJS的filter,中文名“過濾器”是用來過濾變量的值,或者格式化輸出,得到自己所期望的結果或格式的東東,的相關資料,需要的朋友可以參考下
    2015-12-12
  • angular8.5集成TinyMce5的使用和詳細配置(推薦)

    angular8.5集成TinyMce5的使用和詳細配置(推薦)

    這篇文章主要介紹了angular8.5集成TinyMce5的使用和詳細配置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 理解AngularJs篇:30分鐘快速掌握AngularJs

    理解AngularJs篇:30分鐘快速掌握AngularJs

    這篇文章主要介紹了理解AngularJs篇:30分鐘快速掌握AngularJs,詳細介紹了AngularJs所涉及的知識點,有興趣的可以了解一下。
    2016-12-12
  • AngularJS ng-bind-html 指令詳解及實例代碼

    AngularJS ng-bind-html 指令詳解及實例代碼

    本文主要是對AngularJS ng-bind-html 指令知識的詳細講解,并附代碼實例,有需要的小伙伴可以參考下
    2016-07-07
  • AngularJS實現(xiàn)表單驗證功能

    AngularJS實現(xiàn)表單驗證功能

    這篇文章主要為大家詳細介紹了AngularJS實現(xiàn)表單驗證功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 詳解AngularJS中module模塊的導入導出

    詳解AngularJS中module模塊的導入導出

    本文給大家介紹angularjs中module模塊的導入導出,涉及到angularjs module相關知識,對angularjs module感興趣的朋友一起看看吧
    2015-12-12
  • 詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案

    詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案

    這篇文章主要介紹了詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07

最新評論