【问题标题】:Angular Material Dialog modal error角度材料对话框模态错误
【发布时间】:2017-10-26 10:48:33
【问题描述】:

这是第一次使用 angular 4 和材料设计,我试图用 angular material design 创建一个模态窗口并拥有这两个文件:

roomlist.component

import { Component, OnInit, Inject } from '@angular/core';
import { RoomService } from '../../services/room.service';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { RoomDialogComponent } from './dialogs/roomDialogs/room.dialog';


@Component({
  selector: 'app-roomlist',
  templateUrl: './roomlist.component.html',
  styleUrls: ['./roomlist.component.css']
})
export class RoomlistComponent implements OnInit {
  public rooms: any;
  public roomsFilter = {
    date: new Date,
    aviable: 'all',
    globalSeach: ''
  };
  constructor(public roomService: RoomService, public dialog: MatDialog) {
  }

  ngOnInit() {
    this.getData();
  }

  public getData(): any {
    console.log(this.roomsFilter.date);
    this.roomService.getRooms({ date: 'now' })
      .then((res) => {
        this.rooms = res;
        console.log(this.rooms);
      });
  }

  public openDialog(event, room): void {
    const dialogRef = this.dialog.open(RoomDialogComponent, {
      width: '250px',
      data: room
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log(result);
    });
  }

}

还有这个:room.dialog

import { Component, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

/**
 * @title Dialog Overview
 */

@Component({
  selector: 'app-room-dialog',
  templateUrl: './room.dialog.html',
})
export class RoomDialogComponent {

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

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

}

当我按下触发函数 openDIalog 的按钮时,模式应该打开,但我收到此错误消息 No component factory found for RoomDialogComponent. Did you add it to @NgModule.entryComponents?

任何想法

【问题讨论】:

  • 你在@Inject(MAT_DIALOG_DATA) public data: any) { }中有一个不必要的)
  • 没有多余的) 最后一个来自构造函数,另一个来自@inject()

标签: angular angular-material angular-material2


【解决方案1】:

你按照错误说的做了吗?

你添加到入口组件了吗????

@NgModule({
entryComponents: [
    YourDialogComponent
  ]
})

【讨论】:

  • 是的,就是这样,角度材料文档没有明确说明如何使用某些组件
  • 但它确实问你“你把它添加到@NgModule.entryComponents 了吗?”......就是这样......但我同意你的看法。
  • 对不起,我可能错过了阅读,我看到 declaration 没有 entryComponents
  • 就像我说的那样把它添加到 ngModule
  • @MiguelAngelFrias 确实有一节关于将您的对话框添加到entryComponentsmaterial.angular.io/components/dialog/overview#aot-compilation(如果哈希不起作用,请滚动到 AOT 编译部分)
猜你喜欢
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2020-04-24
  • 2021-11-27
相关资源
最近更新 更多