@angular/cli项目构建--modal
环境准备:
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()" >×</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的更多相关文章
- @angular/cli项目构建--组件
环境:nodeJS,git,angular/cli npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm instal ...
- @angular/cli项目构建--Dynamic.Form
导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...
- @angular/cli项目构建--animations
使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...
- @angular/cli项目构建--interceptor
JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...
- @angular/cli项目构建--路由3
路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...
- @angular/cli项目构建--httpClient
app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...
- @angular/cli项目构建--路由2
app.module.ts update const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {p ...
- @angular/cli项目构建--路由1
app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angu ...
- @angular/cli项目构建--Dynamic.Form(2)
form-item-control.service.ts update @Injectable() export class FormItemControlService { constructor( ...
随机推荐
- linux各个文件夹作用
linux /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/ho ...
- Mapinfo修改道路方向
在mapinfp工具管理添加插件Reverse Line Direction,就可以修改道路方向 插件
- C#中时间的Ticks属性
C#中时间的Ticks属性是一个很大的长整数,单位是 100 毫微秒.表示自 0001 年 1 月 1 日午夜 12:00:00 以来已经过的时间的以 100 毫微秒为间隔的间隔数,已经说得很清楚了, ...
- Django 进阶篇之 CSRF,COOIKE,SESSION
1.CSRF(Cross Site Request Forgery, 跨站域请求伪造) CSRF 背景与介绍 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网 ...
- OpenGL学习进程(6)第四课:点、边和图形(一)点
本节是OpenGL学习的第四个课时,下面介绍OpenGL点的相关知识: (1)点的概念: 数学上的点,只有位置,没有大小.但在计算机中,无论计算精度如何提高,始终不能表示一个无穷小的点 ...
- 线程同步synchronized和ReentrantLock
一.线程同步问题的产生及解决方案 问题的产生: Java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),将会导致数据不准确,相互之间产生冲突. 如下例:假设有一个卖票 ...
- 生信基础概念之unique reads VS multi-mapping reads
unique reads:在参考组上只有一个匹配点 multi-mapping reads:在参考组上有多个匹配点 下面是tophat的一个结果案例: Reads: Input : Mapped : ...
- 三 ip dns等配置
一IP.端口.协议基本概念 ip的简单概念 互联网上的计算机,都会有一个唯一的32位的地址,ip地址 我们访问服务器,就必须通过ip地址 局域网里也有预留的ip地址 192/10/172.居于王的i ...
- MySQL备份账号权限
grant select,show view,lock tables,trigger on confluence.* to 'DBbackup'@'127.0.0.1' identified by ' ...
- create_workqueue和create_singlethread_workqueue【转】
本文转载自:http://bgutech.blog.163.com/blog/static/18261124320116181119889/ 1. 什么是workqueueLinux中的Workque ...