【发布时间】:2017-10-11 10:47:51
【问题描述】:
我正在使用材料 2 版本 2.0.0-beta.12。我需要调整材质 Modal 的位置。我按照这个答案来做到这一点。 https://stackoverflow.com/a/44238699/8253445 由于 MdDialog 现在不可用,我使用了 {MatDialog, MatDialogRef, MAT_DIALOG_DATA}。
constructor(public dialog: MatDialog,public dialModRef:MatDialogRef<any>) {}
当我将 MatDialogRef 添加到我的构造函数时,它会给我“No provider for MatDialogRef”错误。请问你能帮我解决这个问题吗?
dialog-overview-example.ts
import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from
'@angular/material';
@Component({
selector: 'dialog-overview-example',
templateUrl: 'dialog-overview-example.html'
})
export class DialogOverviewExample {
animal: string;
name: string;
constructor(public dialog: MatDialog,public
dialModRef:MatDialogRef<any>) {}
openDialog(): void {
let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '500px',
data: { name: this.name, animal: this.animal }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
}
@Component({
selector: 'dialog-overview-example-dialog',
templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) { }
onNoClick(): void {
this.dialogRef.close();
}
}
app.module.ts
entryComponents: [DialogOverviewExample,DialogOverviewExampleDialog]
dialog-overview-example-dialog.html
<h1 md-dialog-title>Hi {{data.name}}</h1>
<div md-dialog-content>
<p>What's your favorite animal?</p>
<md-form-field>
<input mdInput tabindex="1" [(ngModel)]="data.animal">
</md-form-field>
</div>
<div md-dialog-actions>
<button md-button tabindex="2">Ok</button>
<button md-button (click)="onNoClick()" tabindex="-1">No Thanks</button>
</div>
【问题讨论】:
-
而不是
any,给出您要在对话框中打开的组件类的名称。然后,在你的 entryComponents 中添加组件类。 -
我现在这样做了..我仍然得到同样的错误:(
标签: angular angular-material angular-material2