角度2和材质设计 - 示例对话框为空

时间:2017-03-15 16:09:41

标签: angular dialog modal-dialog material-design

我正在尝试创建一个对话框,如材料设计页面Here中所示。我可以显示按钮,单击它,对话框显示,页面按预期变暗但没有文本显示。我觉得我很接近,但我无法弄清楚我哪里出错了。感谢任何帮助,谢谢!

这是我的代码:

    import { Component, OnInit } from '@angular/core';
import {MdDialog} from '@angular/material';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(public dialog: MdDialog) { }

  openDialog() {
    this.dialog.open(DialogOverviewExampleDialog);
  }

  ngOnInit() {
  }

}


@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: './dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {}

1 个答案:

答案 0 :(得分:13)

您的尝试中可能缺少一些内容。

首先在代码中,确保在android.hardware.type.automotive类构造函数中包含MdDialogRef,例如所以,根据documentation

DialogOverviewExampleDialog

其次,对于错误:

  

找不到DialogOverviewExampleDialog的组件工厂。你是否   将它添加到@ NgModule.entryComponents?

您的import {Component} from '@angular/core'; import {MdDialog, MdDialogRef} from '@angular/material'; // Added "MdDialogRef" @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent { constructor(public dialog: MdDialog) {} openDialog() { let dialogRef = this.dialog.open(DialogOverviewExampleDialog); // If you need a result from your dialog dialogRef.afterClosed().subscribe(result => { // Do something with result, if needed. }); } } @Component({ selector: 'dialog-overview-example-dialog', templateUrl: './dialog-overview-example-dialog.html', }) export class DialogOverviewExampleDialog { // Added Constructor constructor(public dialogRef: MdDialogRef<DialogOverviewExampleDialog>) {} } 必须在 两个地方 中定义对话框组件(app.module.ts):DialogOverviewExampleDialog declarations

例如:

entryComponents