@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( ...
随机推荐
- 爬虫基本库之beautifulsoup
一.beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- python常用模块——sys模块
sys模块的功能很多,下面介绍几个常用的模块. 1.sys.argv:从外部向程序内部传递参数 #!/usr/bin/env python import sys print(sys.argv[0]) ...
- Python框架之Tornado(二)预备知识epoll最好的讲解
问:epoll 或者 kqueue 的原理是什么?为什么 epoll 和 kqueue 可以用基于事件的方式,单线程的实现并发?我没看过 linux 内核,对这方面一直有疑问…… 必须从很多基础的概念 ...
- Linux权限管理 chattr命令、lsattr命令、sudo命令
chattr命令 chattr命令用来修改文件系统的权限属性 chatrr 只有 root 用户可以使用,用来修改文件系统的权限属性,建立凌驾于 rwx 基础权限之上的授权. chatrr 命令格式如 ...
- eclipse新建自定义EL函数
==================================================================================================== ...
- 请求静态文件,返回http状态码405,not allowed
昨天在首页加了一个链接,点击这个a标签,会进入http://121.43.68.40/boxpro/template/addsite.pdf,测试环境完全没有问题,上传到正式服务器之后,点击A标签,死 ...
- R读取大数据data.table包之fread
>library(data.table)>data=fread("10000000.txt")>Read 9999999 rows and 71 (of 71) ...
- 20165101刘天野 2017-2018-2 《Java程序设计》第8周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章Java多线程机制 一.进程与线程 1.1.任务调动 大部分操作系统(如Wi ...
- GDKOI2017游记
去年的GDKOI在寒假(虽然我没参加),但由于一些机♂缘♂巧♂合♂,比赛时间变成了开学之后的第一周,于是直接导致了当我的同学们在认真学习的同时,我在广州无所事事地和同学谈♂笑♂风♂生♂.(太好了,可以 ...
- nginx 反向代理配置之---指定单域名
server { listen 80; server_name ngin服务器所对应的的域名; error_log /data/logs/nginx/mainsite.error.log; acces ...