简体   繁体   English

如何在 Angular Material 对话框组件中显示组件?

[英]How to show a component inside Angular Material dialog component?

I would like to show parentComponent that generating c3 charts, inside DialogModalComponent.我想在 DialogModalComponent 中展示生成 c3 图表的 parentComponent。 I can see the text from parentComponent when I open the dialog, but it's not showing the c3 Chart.当我打开对话框时,我可以看到来自 parentComponent 的文本,但它没有显示 c3 图表。 How can I show the c3 chart that's been created in parent component to show in Dialog ModalComponent?如何显示在父组件中创建的 c3 图表以显示在 Dialog ModalComponent 中?

There is an error in your stackblitz.您的堆栈闪电战中存在错误。
At your app.module change to:在您的 app.module 更改为:

import { MatDialogModule } from '@angular/material/dialog';

Now the real question.现在是真正的问题。 In your parent component, when generating your graphic you are selecting the div with an id as seen here:在您的父组件中,生成图形时,您正在选择带有 id 的 div,如下所示:

var chart = c3.generate({
  bindto: '#barChart',
  ...
}

The problem is: when reusing this component, the c3 generator will try to find an id barChart in you html.问题是:当重用这个组件时,c3 生成器会尝试在你的 html 中找到一个 id barChart Turns out it is find the first one that is already showing up and not the new one inside the modal.结果是找到第一个已经出现的,而不是模态中的新的。
To fix this, I think the @ViewChild can help:为了解决这个问题,我认为@ViewChild可以提供帮助:

In parent.component.html:在 parent.component.html 中:
<div #barChart></div>

And in parent.component.ts:在 parent.component.ts 中:

export class ParentComponent implements OnInit {
  @ViewChild('barChart') barChart:ElementRef;
  ...
  createChart(){
    var chart = c3.generate({
    bindto: this.barChart.nativeElement,
    ...
  }  

This way you component always will find it's own barChart这样你的组件总是会发现它自己的 barChart

No provider for MatDialog! MatDialog 没有提供程序!

Please check your app.module.ts the import statement is wrong.请检查您的app.module.ts导入语句是否错误。

import { MatDialogModule } from '@angular/material';

Please check the official documentation of Angular Material design请查看Angular Material design的官方文档

import {MatDialogModule} from '@angular/material/dialog';

and in your component file并在您的组件文件中

import { MatDialog } from '@angular/material/dialog';

Rest of the connectivity is fine .其余的连接很好。 Hope this will solve your problem Cheers!希望这能解决你的问题干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM