Angular2中Bootstrap界面库ng-bootstrap详解

所属分类: 网络编程 / JavaScript 阅读数: 2011
收藏 0 赞 0 分享

准备 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 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 的样式表关联进来:

<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 , 类名则统一使用 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 , 相信已经由很多人蠢蠢欲动了吧!以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

Angular使用Md5加密的解决方法

这篇文章主要介绍了Angular使用Md5加密的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JS构造函数中this和return

本文通过实例代码给大家介绍了JS构造函数中this和return,需要的朋友参考下吧
收藏 0 赞 0 分享

ES6中Array.find()和findIndex()函数的用法详解

ES6为Array增加了find(),findIndex函数。find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined,而findIndex()函数也是查找目标元素,找到就返回元素的位置,找不到就返回-1。下面通过实例详解,需要的朋友参考下吧
收藏 0 赞 0 分享

JS闭包的几种常见形式实例详解

本文通过实例代码给大家详细介绍了js闭包的几种常见形式,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
收藏 0 赞 0 分享

ES6中Array.copyWithin()函数的用法实例详解

ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去。下面重点给大家介绍ES6中Array.copyWithin()函数的用法,需要的朋友参考下
收藏 0 赞 0 分享

Javascript 严格模式use strict详解

严格模式:由ECMA-262规范定义的JavaScript标准,对javascrip的限制更强。这篇文章主要介绍了Javascript 严格模式use strict详解 ,需要的朋友可以参考下
收藏 0 赞 0 分享

引入JavaScript时alert弹出框显示中文乱码问题

今天在HTML中引入JavaScript文件运行时,alert弹出的提示框中文显示为乱码,怎么解决此问题呢?下面小编给大家带来了引入JavaScript时alert弹出框显示中文乱码问题的解决方法,一起看看吧
收藏 0 赞 0 分享

AngularJs 延时器、计时器实例代码

这篇文章主要介绍了AngularJs 延时器、计时器实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

JS分页的实现(同步与异步)

这篇文章主要介绍了JS分页的实现(同步与异步),需要的朋友可以参考下
收藏 0 赞 0 分享

Angularjs自定义指令实现分页插件(DEMO)

由于最近的一个项目使用的是angularjs1.0的版本,涉及到分页查询数据的功能,后来自己就用自定义指令实现了该功能,下面小编把实例demo分享到脚本之家平台,需要的朋友参考下
收藏 0 赞 0 分享
查看更多