我正在开发一个 POC 应用程序,我正在尝试让
MdDialog
组件正常工作。有谁有一个可以传递给 MdDialog
open 方法的工作示例吗?
md
前缀更改为mat
导入
MatDialogModule
而不是 MdDialogModule
@angular/material
现在依赖于 @angular/cdk
作为对等依赖。
回顾:Plunkr
材质对话框 + 附录的 8 个步骤
第1步: 安装包
npm i --save @angular/material @angular/cdk @angular/animations
第2步: 配置
systemjs.config.js
map: {
...
'@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
'@angular/material': 'npm:@angular/material/bundles/material.umd.js',
'@angular/cdk': 'https://unpkg.com/@angular/cdk/bundles/cdk.umd.js',
'@angular/cdk/a11y': 'https://unpkg.com/@angular/cdk/bundles/cdk-a11y.umd.js',
'@angular/cdk/bidi': 'https://unpkg.com/@angular/cdk/bundles/cdk-bidi.umd.js',
'@angular/cdk/coercion': 'https://unpkg.com/@angular/cdk/bundles/cdk-coercion.umd.js',
'@angular/cdk/collections': 'https://unpkg.com/@angular/cdk/bundles/cdk-collections.umd.js',
'@angular/cdk/keycodes': 'https://unpkg.com/@angular/cdk/bundles/cdk-keycodes.umd.js',
'@angular/cdk/observers': 'https://unpkg.com/@angular/cdk/bundles/cdk-observers.umd.js',
'@angular/cdk/overlay': 'https://unpkg.com/@angular/cdk/bundles/cdk-overlay.umd.js',
'@angular/cdk/platform': 'https://unpkg.com/@angular/cdk/bundles/cdk-platform.umd.js',
'@angular/cdk/portal': 'https://unpkg.com/@angular/cdk/bundles/cdk-portal.umd.js',
'@angular/cdk/rxjs': 'https://unpkg.com/@angular/cdk/bundles/cdk-rxjs.umd.js',
'@angular/cdk/scrolling': 'https://unpkg.com/@angular/cdk/bundles/cdk-scrolling.umd.js',
'@angular/cdk/table': 'https://unpkg.com/@angular/cdk/bundles/cdk-table.umd.js',
'@angular/cdk/stepper': 'https://unpkg.com/@angular/cdk/bundles/cdk-stepper.umd.js',
},
第3步: 将
MatDialogModule
导入您的模块
import { MatDialogModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule, <== required
MatDialogModule <== here
],
declarations: [ AppComponent],
bootstrap: [ AppComponent ]
})
export class AppModule {}
第四步: 创建所需的对话框组件,例如:
@Component({
selector: 'your-dialog-selector',
template: `
<h2>Hi! I am modal dialog!</h2>
<button mat-raised-button (click)="dialogRef.close()">Close dialog</button>`
})
export class DialogComponent {
constructor(public dialogRef: MdDialogRef<DialogComponent>) { }
}
第5步: 将步骤 4 中的组件添加到 NgModule 装饰器的
declarations
和 entryComponents
数组中:
@NgModule({
imports: [ BrowserModule, MatDialogModule ],
declarations: [ AppComponent, DialogComponent ],
entryComponents: [ DialogComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
第6步: 在您的组件中使用它:
@Component({
selector: 'my-app',
template: `<button mat-raised-button (click)="openDialog()">Open dialog</button>`,
})
export class App {
constructor(public dialog: MatDialog) { }
openDialog(key) {
let dialogRef = this.dialog.open(DialogComponent);
}
}
第7步 选择所需的主题:
<link href="https://unpkg.com/@angular/material/prebuilt-themes/deeppurple-amber.css"
rel="stylesheet">
您可以在这里找到其他主题
第8步
如果您想将数据传递给模态,请使用以下命令(Plunker):
dialogRef.componentInstance.param1 = "test value";
附录
路由对话框:Plunkr
可拖动对话框(如何制作 MatDialog 可拖动/角度材质)
另请参阅
今天,您可以在这里找到大量可用的
MatDialog
示例:请注意,您可以通过多种方式将值传递到对话框:
MAT_DIALOG_DATA
this.dialogRef.componentRef.setInput(key, value)