模态框是项目中经常会用到的一个公共功能,通常会被用左提示框或者扩展选项框。

下面,我用一个小例子来简单展示实现模态框功能的过程:

1、为项目加包:

ng add ngx-bootstrap

2、在xxx.module.ts(模块.ts文件)中引入:

...
import { ModalModule } from "ngx-bootstrap/modal";
... @NgModule({
imports: [
...
ModalModule.forRoot(),
...
]
})
...

3、创建一个模块框公共组件:

//.ts部分

import { Component } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap'; @Component({
selector: 'response-modal',
templateUrl: './response-modal.html'
}) export class ResponseModalService {
public header: string;
public body: string; constructor(public modalRef: BsModalRef
) {} }
<!--  模态框模板部分 .html -->

<style>
.centerize {
text-align: center;
}
</style> <div class="container-fluid"><!-- 模态框容器样式 -->
<div class="modal-header"><!-- 框头样式 -->
<h5>{{ header }}</h5>
<button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()"> <!-- 关闭按钮样式 -->
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" style="white-space: pre-line;"><!-- 框内容样式 -->
{{ body }}
</div>
<div class="centerize">
<button type="button" text-aling="center" class="btn btn-primary mr-2" (click)="modalRef.hide()">OK</button><!-- 常规按钮样式 -->
</div>
<p>
</div>

4、在xxx.page.ts(模版.ts文件)中引入并,并调用:

import { BsModalRef, BsModalService } from "ngx-bootstrap";//引入模态框资源对象及服务对象
import { ResponseModalService } from "src/app/shared/component/response-modal";//引入上面创建好的模态框组件
...

public modalRef: BsModalRef;
... constructor(
private modalService: BsModalService,
...
) {
...
}
... public openUpdateNotification(message: string): void {
this.modalRef = this.modalService.show(ResponseModalService, {//初始化一个之前创建好的模态框组件
initialState: {
header: "",
body: message
},
class: "modal-lg"
});
}

5、在合适位置调用打开模态框的方法openUpdateNotification即可。

最新文章

  1. SQL Server 2008 R2 错误代码:233
  2. MyBatis学习总结(一)&mdash;&mdash;MyBatis快速入门
  3. 基于.net开发chrome核心浏览器【五】
  4. Bootstrap系列 -- 2. 标题
  5. 不正确使用WeakHashMap引起的卡死
  6. oracle查看所有表的数据量并排序
  7. 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了
  8. 【如何快速的开发一个完整的iOS直播app】(推流篇)
  9. PS拾色器(前景色背景色)快捷键
  10. 简要地写出一个.NET&#160;Remoting的示例
  11. android 欢迎界面的淡入效果
  12. 微信,QQ这类IM app怎么做——谈谈Websocket
  13. 一个周末掌握IT前沿技术之node.js篇
  14. 开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台
  15. 谷歌(Google Chrome)插件安装
  16. Java微信公众平台开发_06_素材管理
  17. Java基础系列--final关键字
  18. UI设计切忌墨守成规,但改变也须用数据说话
  19. Android:剖析源码,随心所欲控制Toast显示
  20. html的基础属性

热门文章

  1. codeforces Summer Earnings(bieset)
  2. [NOI2015]程序自动分析(并查集,离散化)
  3. JSON.stringify常见用法
  4. CBV和FBV
  5. Baltic2014 sequence
  6. http各个状态码的含义
  7. mycat操作mysql示例之分库
  8. web应用,http协议简介,web框架
  9. [hadoop](2) MapReducer:Distributed Cache
  10. MainActivity.java 文件