【问题标题】:MatDialog not displayed correctlyMatDialog 未正确显示
【发布时间】:2018-04-24 17:58:02
【问题描述】:

我正在尝试使用MatDialog 并基于this example 我已按如下方式实现了该对话框:

import {Component, Inject, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AuthenticationService} from '../../../service/authentication.service';
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material';

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

  constructor(private router: Router, public dialog: MatDialog) { }

  openDialog(): void {
    const dialogRef = this.dialog.open(CreateGroupDialogComponent);
  }    
}

@Component({
  selector: 'app-create-group-dialog',
  template: `
    <span>Hello World!</span>
  `
})
export class CreateGroupDialogComponent {
  constructor(public dialogRef: MatDialogRef<CreateGroupDialogComponent>) {  }
}

然而,即使当我按下相应的按钮时出现对话框,我得到的却是:

HTML 模板未显示,并且模态框的尺寸错误。

我不明白问题出在哪里。为什么模态没有正确绘制?

这是打开模式时生成的 HTML 代码。可以看出它是空的。

【问题讨论】:

  • 当您使用mat-dialog-actions 参考时,您可能需要mat-dialog-content。见material.angular.io/components/dialog/overview#dialog-content
  • @flamusdiu 我不认为这是一个要求,但即使我添加它或只是显示一个&lt;span&gt;Hello World!&lt;/span&gt; 它仍然是相同的结果。
  • 你的 CSS 中有什么?空吗?
  • @flamusdiu 是的,不涉及额外的 CSS。
  • 它看起来认为它是一个侧导航选项卡。让我在一个新项目中尝试一下。

标签: angular


【解决方案1】:

我遇到了这个问题,通过 ngZone.run() 方法解决了。 我正在使用 Angular 13,所以不需要那些 entryComponents

所以我有一个页面可以在回调函数(如反馈表)上打开一个对话框(单独的组件)

但该对话框不会呈现所有与角度相关的组件,而是仅呈现在refund-popup.component.html 和refund-popup 中编写的Html CSS。组件.css

这是因为回调来自角度区域外。所以我只是让我的对话框在区域内运行。

解决方案

import { NgZone} from '@angular/core'
import { MatDialog } from '@angular/material/dialog';
import { RefundPopupComponent } from 'src/app/components/refund-popup/refund-popup.component';



@Component({
selector: 'app-create-profile',
templateUrl: './create-profile.component.html',
styleUrls: ['./create-profile.component.scss'],
})

export class CreateProfileComponent {
   constructor(private zone: NgZone , private dialog: MatDialog) {}
   
   someFunction() {
       this.zone.run(() => this.dialog.open(RefundPopupComponent}))

    }     
}

在包含 ngZone 运行方法之后,我已经更新了前后图像

【讨论】:

    【解决方案2】:

    更新 - 2021 年 4 月

    使用 Ivy,解决此问题的方法不再是将您的组件添加到 entryComponents。我遇到了这个问题,更令人困惑的是,它在开发和生产中都可以在本地运行,但在我们部署的环境中却没有。它只是默默地失败了。在将头撞到墙上一整天后,我发现了一个循环依赖项,由于某种原因没有引发错误。删除此依赖项后,一切都会按预期进行。

    【讨论】:

      【解决方案3】:

      使用自 9.0 以来的新常春藤编译器,entryComponents 字段不再需要并且已被弃用。

      我只是通过重新启动我的ng serve 解决了这个问题。

      【讨论】:

        【解决方案4】:

        您需要将其添加到您的@NgModule 上的entryComponents

        import { BrowserModule } from '@angular/platform-browser';
        import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
        import { NgModule } from '@angular/core';
        import { MatDialogModule, MatButtonModule } from '@angular/material';
        
        import { AppRoutingModule } from './app-routing.module';
        
        import { AppComponent } from './app.component';
        import { LoginComponent } from './login/login.component';
        import { HomeComponent, DialogOverviewExampleDialogComponent } from './home/home.component';
        
        
        @NgModule({
          declarations: [
            AppComponent,
            LoginComponent,
            HomeComponent,
            DialogOverviewExampleDialogComponent
          ],
          imports: [
            BrowserModule,
            BrowserAnimationsModule,
            AppRoutingModule,
            MatDialogModule,
            MatButtonModule
          ],
          providers: [],
          bootstrap: [AppComponent],
          entryComponents: [
            DialogOverviewExampleDialogComponent
          ]
        })
        export class AppModule { }
        

        Angular2 material dialog has issues - Did you add it to @NgModule.entryComponents?的副本

        【讨论】:

        • 是的,这解决了问题 - 谢谢!我刚刚看到documentation 中实际上提到了这一点(我一定在那里忽略了它)但我在plunker 示例中没有看到。
        • FWIW,它在控制台中给你一个错误,当你打开对话框并且它丢失时告诉你它。
        • 你先生是个传奇!像魅力一样工作
        • 非常感谢您的解释。只是想补充一下:在6.x.x及更高版本中,它似乎没有显示任何关于EntryComponent的错误
        • 升级到 Angular 9 后还有其他人遇到此问题,并且他们的文档说您不再需要声明“entryComponents”吗?
        猜你喜欢
        • 2019-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多