angular – 没有MatDialogRef的提供者

前端之家收集整理的这篇文章主要介绍了angular – 没有MatDialogRef的提供者前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用材料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添加到我的构造函数时,它给了我“没有MatDialogRef的提供程序”错误.请你帮我搞清楚吗?

对话框的概述,example.ts

import {Component,Inject} from '@angular/core';
import {MatDialog,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.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>
您需要在模块文件中导入此模块(默认情况下为app.module.ts):
import { MatDialogModule } from '@angular/material';

并将MatDialogModule添加到模块声明中的导入:

@NgModule({
  declarations: [...],imports: [
    ...
    MatDialogModule,...
  ],providers: [...],entryComponents: [...],bootstrap: [...]
})

编辑:

您需要通过this.dialogRef.close()传递数据;获得订阅工作的结果.例如:

@Component({
   selector: 'dialog-overview-example-dialog',})
export class DialogoverviewExampleDialog {
someValue: boolean = false;

constructor(
  public dialogRef: MatDialogRef<DialogoverviewExampleDialog>,@Inject(MAT_DIALOG_DATA) public data: any) { }

 onNoClick(): void {
   this.dialogRef.close(this.someValue);
 }

}

猜你在找的Angularjs相关文章