环境准备:

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. Python报错——module 'scipy.misc' has no attribute 'imresize'

    报错是因为要安装PIL库,库名现在为Pillow,在命令行上安装即可: pip3 install Pillow

  2. 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related(转)

    转至:http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html application/x-www-form-urlencoded: 窗 ...

  3. $《第一行代码:Android》读书笔记——第6章 数据持久化

    主要讲述了Android数据持久化的三种方式:文件存储.SharedPreference存储.SQLite数据库存储. (一)文件存储 其实Android中文件存储方式和Java的文件操作类似,就是用 ...

  4. sql中in和exists的区别效率问题 转

    in 和exists in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果 ...

  5. shell中的$()、${}、$(())、(())

    $( ) 与 ` ` (反引号)在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换用(command substitution)的. 所谓的命令替换与我们第五章学过的变 ...

  6. awk遇到windows 的^M

    windows在编辑的文档,在linux中显示会在行尾出现一个^M window下编辑的文档:末尾带^M$ linux下编辑的文档:末尾带$ awk中如果存在^M,则会限制print的输出列数(只能输 ...

  7. 淘宝分类常见---部分显示和全部显示的js效果

    需求就是,点击“更多按钮”,显示全部的分类详情,再次点击,显示部分分类. 展开: 收起: 结构: <div class="SubBox" id="SubBox&qu ...

  8. 【转载】丑数humble numbers

    转载地址:http://blog.csdn.net/qwerty_xk/article/details/12749961 题:只有2 3 5 这三个因子的数,求第1500个   设1为第一个丑数,求第 ...

  9. redis中文文档

    phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...

  10. UVA 11525 Permutation (树状数组+YY)

    题意:给你k个数Si,然后给你一个等式   H= ∑  Si ∗ (K − i)!  (i=(1->k)且0 ≤ Si ≤ K − i). 叫你求出第H个全排列 其实这是一个康托展开:X=a[n ...