@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( ...
随机推荐
- C# emoji 表情如何插入mssql
如何将emoji表情存入mssql 呢? 在Windows显示emoji(win7需要安装补丁) 在MAC完美支持 步骤就是将显示不出来的emoji UrlEncode=>进入MSsql 然后拿 ...
- 【整理学习Hadoop】H D F S 一个分布式文件系统
Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统的区别 ...
- SpringMVC:学习笔记(5)——数据绑定及表单标签
SpringMVC——数据绑定及表单标签 理解数据绑定 为什么要使用数据绑定 基于HTTP特性,所有的用户输入的请求参数类型都是String,比如下面表单: 按照我们以往所学,如果要获取请求的所有参数 ...
- Redis慢查询,redis-cli,redis-benchmark,info
一.慢查询: 1.慢查询的作用:通过慢查询分析,找到有问题的命令进行优化. 2.慢查询的redis的配置参数: slowlog-log-slower-than 慢查询预设阈值(单位是微秒1秒=1000 ...
- 前端 css续
CSS选择器 1.标签选择器 为类型标签设置样式例如:<div>.<a>.等标签设置一个样式,代码如下: <style> /*标签选择器,找到所有的标签应用以下样式 ...
- 自己动手编译Android源码(超详细)
http://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈android源码编译的一些事. ...
- Python编程-数据库
1.MySQL 程序: socket客户端 根据IP和端口进行连接 发送指令: xxx 接收结果 socket服务端 运行起来 获取指令(recv) xxx 解析命令 文件夹操作: ... 文件操作: ...
- for update排他锁详解
使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...
- INSPIRED启示录 读书笔记 - 第19章 用户体验设计与实现
先定义用户体验再动手开发 在软件开发过程中,有很多工作可以同时进行.比如,需求调研和产品设计(用户体验设计).开发与测试 尽管如此,用户体验设计和软件开发就不能同时进行,原因有五点 1.与软件开发团队 ...
- Threalocal的使用及其原理
虽然现在可以说很多程序员会用ThreadLocal,但是我相信大多数程序员还不知道ThreadLocal,而使用ThreadLocal的程序员大多只是知道其然而不知其所以然,因此,使用ThreadLo ...