Angular2+之模态框-使用ngx-bootstrap包中的模态框组件实现
2024-10-20 03:35:15
模态框是项目中经常会用到的一个公共功能,通常会被用左提示框或者扩展选项框。
下面,我用一个小例子来简单展示实现模态框功能的过程:
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即可。
最新文章
- SQL Server 2008 R2 错误代码:233
- MyBatis学习总结(一)&mdash;&mdash;MyBatis快速入门
- 基于.net开发chrome核心浏览器【五】
- Bootstrap系列 -- 2. 标题
- 不正确使用WeakHashMap引起的卡死
- oracle查看所有表的数据量并排序
- 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了
- 【如何快速的开发一个完整的iOS直播app】(推流篇)
- PS拾色器(前景色背景色)快捷键
- 简要地写出一个.NET&#160;Remoting的示例
- android 欢迎界面的淡入效果
- 微信,QQ这类IM app怎么做——谈谈Websocket
- 一个周末掌握IT前沿技术之node.js篇
- 开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台
- 谷歌(Google Chrome)插件安装
- Java微信公众平台开发_06_素材管理
- Java基础系列--final关键字
- UI设计切忌墨守成规,但改变也须用数据说话
- Android:剖析源码,随心所欲控制Toast显示
- html的基础属性