【问题标题】:Angular 8: max-height attribute for a material dialog is not workingAngular 8:材质对话框的最大高度属性不起作用
【发布时间】:2020-02-18 21:25:00
【问题描述】:

我正在开发一个 Angular 8 的项目,我使用 MatDialog 在对话框中打开带有表单输入的新组件。例如:

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

import {AddDialogComponent} from '../add-dialog.component';

constructor(private dialog: MatDialog){
}

private addNewRow() {

    const dialogRef = this.dialog.open(AddDialogComponent, {

      width: 'auto',
      height: 'auto',
      maxHeight: '100vh',
      maxWidth:'100vw',
      data: Data
    });
    dialogRef.afterClosed().subscribe(
      result => {

         // statements
      });
  }

当对话框打开时,ma​​xWidth: '100vw' 可以正常工作,因为不支持 ma​​xHeight: '100vh'。还尝试了 ma​​xHeight: '100vh !important' 并尝试从 .css 文件更改样式。

两者都不起作用。对此问题的任何建议或解决方案将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript html css angular angular-material


    【解决方案1】:

    这是因为.mat-dialog-content 有一个max-height: 65vh。您可以决定不使用该指令,或者在您的 css 中覆盖它:

    .mat-dialog-content {
      max-height: initial;
    }
    

    working example

    【讨论】:

    • 我知道。毕竟我也做了你提到的改变。但需要为 .mat-dialog-container 设置“最大高度”。
    【解决方案2】:

    API 建议在 maxWidthmaxHeight 内可以使用数字或字符串...

    但是我们可以使用::ng-deep 将其放入 CSS 中会有所帮助...请注意,渲染后的 .cdk-overlay-pane 的内联最大宽度为 80%,因此我们必须使用 100% !important 否则,我们可以通过 CSS 做到这一点

    相关 CSS

    ::ng-deep .cdk-overlay-pane { max-width: 100vw !important; max-height: 100vh; }
    ::ng-deep .cdk-overlay-pane .mat-dialog-container{
      background-color:lightgreen;
      max-width: 100vw;
      max-height: 100vh;
    }
    ::ng-deep .cdk-overlay-pane .mat-dialog-container .mat-dialog-content { max-height: 100%; }
    

    工作stackblitz here

    【讨论】:

    • 除了 "max-height",所有其他属性都有效。在您的示例中尝试在
      中添加更多文本/内容。滚动条似乎向上向下滚动,但高度没有达到 max-height(100vh)。
    • 嘿@Pujan,试试这个css ::ng-deep .cdk-overlay-pane { max-width: 100vw !important; max-height: 100vh; } ::ng-deep .cdk-overlay-pane .mat-dialog-container{ background-color:lightgreen; max-width: 100vw; max-height: 100vh; } ::ng-deep .cdk-overlay-pane .mat-dialog-container .mat-dialog-content { max-height: 100%; },它也在stackblitz链接上更新......
    【解决方案3】:

    打开对话框时,您可以通过以下方式提供属性,它将全屏打开对话框

    const dialogRef = this.dialog.open(exampleDialog, {
      height: '100vh',
      width: '100vw',
      maxHeight: '100vh',
      maxWidth: '100vw',
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 2020-12-30
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 2020-02-23
      • 1970-01-01
      相关资源
      最近更新 更多