【问题标题】:angular material dialog mat-dialog-actions position角度材质对话框垫对话框动作位置
【发布时间】:2020-02-23 12:32:12
【问题描述】:

我正在使用Angular Material Dialog component,使用 mat-dialog-content 和 mat-dialog-actions 来显示带有操作按钮的页脚。

如果我设置对话框的高度(例如 80%),对话框操作会比默认设置高出奇特。

Here is a forked stackblitz of an official example

我只是设置高度:80%

const dialogRef = this.dialog.open(DialogContentExampleDialog, {
  height: '80%',
  width: '520px'
});

结果如下:

在我看来这是一个问题 :) 但你的意见是什么?是否可以轻松解决?

谢谢!

【问题讨论】:

  • 有这方面的消息吗?我也会感兴趣的

标签: angular angular-material modal-dialog angular-components


【解决方案1】:

Github 问题跟踪此https://github.com/angular/components/issues/4609

包含 DanielHabenicht 的更清洁的修复(无魔法数字) https://github.com/angular/components/issues/4609#issuecomment-528865962

// app.module.ts
import {MAT_DIALOG_DEFAULT_OPTIONS} from '@angular/material';
...
providers: [
    {provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {panelClass: 'mat-dialog-override'}}
]
...


// overrides.scss or styles.scss
// This fixes https://github.com//issues/4609
.mat-dialog-override {
  height: 0px;
  mat-dialog-container {
    > :first-child {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    mat-dialog-content,
    div[mat-dialog-content] {
      flex-grow: 1;
      max-height: unset;
    }
  }
}

【讨论】:

  • 这应该是公认的答案。没有神奇的数字
【解决方案2】:

你可以“拉伸”你的mat-dialog-content。

<h1 mat-dialog-title>Hi {{data.name}}</h1>
<div mat-dialog-content style="height: calc(100% - 96px);">     <-- height of dialog minus title and actions height
  <p>What's your favorite animal?</p>
  <mat-form-field>
    <input matInput [(ngModel)]="data.animal">
  </mat-form-field>
</div>
<div mat-dialog-actions>
  <button mat-button (click)="onNoClick()">No Thanks</button>
  <button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button>
</div>

【讨论】:

  • 是的,我完全按照你的方式做了,我只是不喜欢 calc 中的那个幻数......到目前为止还没有找到更好的解决方案。
  • 非常好的解决方案。效果很好!虽然这是 mat-dialog 的一种奇怪行为,但我必须以这种方式破解它才能让按钮粘在对话框的底部。
【解决方案3】:

对我有用:

.mat-dialog-override {
  mat-dialog-container {
    display: flex;
    flex-direction: column;

    mat-dialog-actions {
      margin-top: auto;
    }
  }
}

【讨论】:

    【解决方案4】:

    @JuNe 的解决方案效果非常好,并且反应灵敏。我唯一需要添加的是我需要进行的小调整才能使其正常工作。我必须明确将 ma​​t-dialog-content 的最大高度设置为未设置:

    style="height: calc(100% - 96px);max-height: unset"
    

    【讨论】:

      【解决方案5】:

      例如,您将mat-dialog-actions 设置为style="position: relative; top: 5rem;"。 top 的值可以根据您的模态高度变化。

      【讨论】:

        【解决方案6】:

        &lt;div mat-dialog-actions style="box-sizing: border-box"&gt; 为我工作!

        【讨论】:

          【解决方案7】:

          因为我们将标题、内容和操作包装在一个表单中,这将解决我们项目的最小高度的粘页脚问题:

          app.module.ts

          import {MAT_DIALOG_DEFAULT_OPTIONS} from '@angular/material';
          ...
          providers: [{
            provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {
              panelClass: 'mat-dialog-override',
              hasBackdrop: true, // enable Backdrop
              disableClose: true // disable click and escape for closing
            }
          }]
          ...
          

          html:

          <ng-template #dialog>
            <form>
              <div mat-dialog-title>Title</div>
              <div mat-dialog-content>...</div>
              <div mat-dialog-actions>...</div>        
            </form>
          </ng-template>
          

          scss:

          .mat-dialog-override {
            mat-dialog-container {
              display: flex;
              flex-direction: column;
          
              > :first-child {
                height: 100%;
                display: flex;
                flex-direction: column;
                flex-grow: 1;
              }
          
              [mat-dialog-content] {
                flex-grow: 1;
                max-height: unset;
              }
            }
          }
          
          

          我们使用材质属性来标记html元素。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-04-01
            • 2021-08-22
            • 2017-11-02
            • 2020-06-29
            • 2017-12-19
            • 2018-06-30
            • 1970-01-01
            相关资源
            最近更新 更多