angular - 没有 MatDialogRef 的提供者

标签 angular angular-material angular-material2

我使用的是 Material 2 版本 2.0.0-beta.12。我需要调整 Material 模态的位置。我按照这个答案来做到这一点。 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

对话框概览示例.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();
    }
}

应用程序模块.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>

最佳答案

你会得到准确的结果

No provider for MatDialogRef

如果您在 dialog-overview-example.html 中包含对话框的组件 html 选择器,则会出错喜欢<dialog-overview-example-dialog></dialog-overview-example-dialog>

没有必要,不要在任何地方包含对话框组件的 html 选择器。

关于angular - 没有 MatDialogRef 的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46686310/

相关文章:

nginx - 配置 Nginx 使 Angular 2 quickstart 与 Browsersync 一起工作

html - 页面在移动 View ( Angular )中打开 NumPad 而不是文本键盘

html - 如何保持用 <md-card>..</md-card> 包裹的图像的纵横比

html - 如何在 Angular Material Design 中使 md 内容可滚动

Angular Material 网格布局

angular - 使用 Material2 MdAutocomplete 组件 - 如何使用 onselect 事件

javascript - Angular 2 : How to pass down a template through a component property?

css - 如何使用 Material 在同一行中制作图标和占位符?

angular - 搜索多选下拉 Angular 4 和 Material 2

javascript - JS NG2 Firebase - 将 foreach 值添加到数组并在模板中使用