环境准备:

cnpm install ngx-bootstrap-modal --save-dev

impoerts: [BootstrapModalModule.forRoot({container: ducument.body})]

usage:

import { Component } from '@angular/core';
import { DialogService } from "ngx-bootstrap-modal"; @Component({
selector: 'app-prooduct-add',
template: `
<div class="container">
<button class="btn btn-default" (click)="dialogService.alert('提醒', '确认要删除吗?');">Show confirm</button>
<button class="btn btn-default" (click)="showConfirm()">Show confirm</button>
</div>
`
})
export class AppComponent {
constructor(public dialogService:DialogService) {}
showConfirm() {
// confirm 返回的是一个 Promise<boolean> 类型,如果在对话框中点【确认】返回 `true`,其他情况返回 `false`
this.dialogService.confirm('提醒', '确认要删除吗?', <BuiltInOptions>{
// 可选项,可以对部分参数重写
}).then((result: boolean) => {
// result
});
}
}

show:

this.dialogService.show(<BuiltInOptions>{
content: '保存成功',
icon: 'success',
size: 'sm',
showCancelButton: false
})

内置模态框包括 alert confirm prompt 三种形态

ngx-bootstrap-modal <BuildOpteions>config:

{
"title": "标题",
"content": "内容",
"icon": "error|warning|success|info|question",
"size": "sm|lg",
"showCloseButton": true,
"input": "text",
"inputValue": "",
"inputPlaceholder": "必填项",
"inputRequired": true,
"inputError": "",
"inputAttributes": {},
"showCancelButton": true,
"cancelButtonText": "取消",
"showConfirmButton": true,
"confirmButtonText": "确认",
"backdrop": false,
"timeout": "3000",
"keyboard": true
}

demo:

app-modal-comfirm.cpmponent.html

<div class="modal-dialog">
<div class="modal-content"> <div class="modal-header">
<button type="button" class="close" (click)="close()" >&times;</button>
<div class="modal-title">{{title || 'confirm'}}</div>
</div> <div class="modal-body">
<p>{{message || 'Are you sure'}}</p>
</div> <div class="modal-footer">
<button type="button" class="btn btn-primary"(click)="confirm()">OK</button>
<button type="button" class="btn btn-default" (click)="close()" >Cancel</button>
</div>
</div>
</div>

app-confirm.component.ts

import {Component, OnInit} from '@angular/core';
import {DialogComponent, DialogService} from 'ngx-bootstrap-modal';
import {Product} from '../../product/product.component'; export interface ConfirmModel {
title: string;
message: string;
} @Component({
selector: 'app-confirm',
templateUrl: './confirm.component.html',
styleUrls: ['./confirm.component.css']
})
export class ConfirmComponent extends DialogComponent<ConfirmModel, Product> implements ConfirmModel {
title: string;
message: string; // 构造函数需要一个DialogService参数
constructor(dialogService: DialogService) {
super(dialogService);
} confirm() {
// result是一个boolean类型,这一点取决于{DialogComponent<ConfirmModel, boolean>}
this.result = new Product(8, 'name_002', 800, 'desc_002', 'category_002');
// close() 方法是由 `DialogComponent` 定义的,用于关闭模态框。在HTML模板中也有可以调用。
this.close();
}
}

app-product.component.ts

import {Component, OnInit} from '@angular/core';
import {DialogService} from 'ngx-bootstrap-modal';
import {BuildOptions} from '@angular/cli/models/build-options';
import {isNullOrUndefined} from 'util';
import * as _ from 'lodash';
import {Product} from '../../product/product.component';
import {ConfirmComponent} from '../../modal/confirm/confirm.component'; @Component({
selector: 'app-product-add',
templateUrl: './product-add.component.html',
styleUrls: ['./product-add.component.css']
})
export class ProductAddComponent implements OnInit { model = new Product(7, 'name', 767, 'desc', 'category'); constructor(private dialogService: DialogService) {
} ngOnInit() {
} onSubmit() {
this.dialogService.show(<BuildOptions>{
content: '保存成功',
icon: 'success',
size: 'sm',
showCancelButton: false
});
this.reset();
} reset() {
this.model.id = null;
this.model.name = null;
this.model.desc = null;
this.model.price = null;
this.model.category = null;
} confirm() {
const disposable = this.dialogService.addDialog(ConfirmComponent, {
title: 'Confirm title',
message: 'Confirm message'
}).subscribe((product) => {
if (!isNullOrUndefined(product)) {
this.model = product;
}
});
// 可以调用 disposable.unsubscribe() 关闭对话框
setTimeout(() => {
disposable.unsubscribe();
}, 10000);
}
}

@angular/cli项目构建--modal的更多相关文章

  1. @angular/cli项目构建--组件

    环境:nodeJS,git,angular/cli npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm instal ...

  2. @angular/cli项目构建--Dynamic.Form

    导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...

  3. @angular/cli项目构建--animations

    使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...

  4. @angular/cli项目构建--interceptor

    JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...

  5. @angular/cli项目构建--路由3

    路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...

  6. @angular/cli项目构建--httpClient

    app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...

  7. @angular/cli项目构建--路由2

    app.module.ts update const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {p ...

  8. @angular/cli项目构建--路由1

    app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angu ...

  9. @angular/cli项目构建--Dynamic.Form(2)

    form-item-control.service.ts update @Injectable() export class FormItemControlService { constructor( ...

随机推荐

  1. MySQL数据库(3)_MySQL数据库表记录操作语句

    附: MYSQL5.7版本sql_mode=only_full_group_by问题 .查询当前sql_mode: select @@sql_mode .查询出来的值为: set @@sql_mode ...

  2. php 跨域 form提交 2种方法

    出于安全因素考虑,直接跨域访问是不允许的,下面介绍二种跨域的方法. 一,通过php curl function curlPost($url,$params) { $postData = ''; for ...

  3. php获取MAC地址

    /** *获取mac地址 **/ class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 var $mac_addr; fun ...

  4. Python自然语言处理 - 系列四

    一 如何使用形式化语法来描述无限的句子集合的结构?    --上下位无关文法       1.1 一个例子:      grammar1 = nltk.parse_cfg(""&q ...

  5. 大话设计模式之PHP篇 - 单例模式

    在编写PHP代码的时候,经常使用new关键字实例化一个对象,比如 <?php Class Database { } $db = new Database; 这是最常规的实例化操作方法,像数据库操 ...

  6. 关于Pytorch的二维tensor的gather和scatter_操作用法分析

    看得不明不白(我在下一篇中写了如何理解gather的用法) gather是一个比较复杂的操作,对一个2维tensor,输出的每个元素如下: out[i][j] = input[index[i][j]] ...

  7. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. 《机器学习基石》第一周 —— When Can Machine Learn?

    (注:由于之前进行了吴恩达机器学习课程的学习,其中有部分内容与机器学习基石的内容重叠,所以以下该系列的笔记只记录新的知识) <机器学习基石>课程围绕着下面这四个问题而展开: 主要内容: 一 ...

  9. maven install 找不到自定义jar包

    解决方案是在Prefernces-->Maven-->user Settings 里面点击Update settings 然后就好了.因为默认你配置settings.xml指向你的类库的时 ...

  10. 【bzoj1899】[Zjoi2004]Lunch 午餐(贪心+dp)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1899 显然为了节省时间,吃饭慢的人要先打饭.于是我们可以先把所有人按吃饭时间排序,于是 ...