【问题标题】:Angular material dialog component hides all my website componentsAngular 材质对话框组件隐藏了我所有的网站组件
【发布时间】:2017-12-12 22:53:23
【问题描述】:

我正在使用 angular 5 和 angular material(最新版​​本),我正在尝试从页面打开一个对话框。当我单击触发打开的按钮时,整个网站都被置于空白背景中,就像对话框将其内容重叠并全部隐藏一样。

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

@Component({
    moduleId: module.id,
    selector: 'app-dialog',
    templateUrl: 'dialog.component.html',
    styleUrls: ['dialog.component.scss']
})
export class DialogComponent implements OnInit {

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

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

    ngOnInit() {
    }

}

这就是打开对话框的方法。

onSubmit() {

        const dialogRef = this.dialog.open(DialogComponent, {
            width: '250px',
            data: { name: 'Juan Manuel', animal: 'Perro' }
        });

        dialogRef.afterClosed().subscribe(result => {
            console.log('The dialog was closed');
            console.log(result);
        });
    }

更新: 我已经看到,在呈现对话框后,一个类被添加到我的 html 标记中。 .cdk-global-scrollblock我不知道为什么那个类被添加到我的html标签中。

.cdk-global-scrollblock {
    position: fixed;
    width: 100%;
    overflow-y: scroll;
}

这是导致我的错误的原因。有人知道为什么我的 html 标签上有那个类吗?

【问题讨论】:

  • 检查控制台,可能会触发一些异常,可能是组件初始化问题。
  • nop,控制台上没有错误对话框打开正常,但我的所有网站都是灰色和空白
  • 上面的类是在哪里添加的。你能用 plunker 复制吗?
  • 我不能使用 plunker,但我已经按照官方 Angular Material 网站的教程进行操作。并且对话框容器在路由器插座内,不知道这是否有帮助
  • 你能告诉我们你的app.module.ts吗?

标签: angular material-design angular-material


【解决方案1】:

最简单的解决方法是将对话框滚动策略设置为新的NoopScrollStrategy 对象:

const dialogRef = this.dialog.open(DialogComponent, {
        width: '250px',
        data: { name: 'Juan Manuel', animal: 'Perro' },
        scrollStrategy: new NoopScrollStrategy()
    });

这只需要额外的导入:

import { NoopScrollStrategy } from '@angular/cdk/overlay';

【讨论】:

  • 我在app.module.ts中直接添加到providers作为默认配置。像这样:` {provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {width: '648px', scrollStrategy: new NoopScrollStrategy()}}`。完美运行
【解决方案2】:

这是由于cdk-global-scrollblock 被注入到 HTML body 中,这将影响具有绝对位置的组件。

您可以在 Angular Material 主题 CSS 中覆盖它:

.cdk-global-scrollblock {
    position: static;
    overflow: hidden !important;
}

或已弃用的暗影穿刺:

/deep/ .cdk-global-scrollblock {
    position: static;
    overflow: hidden !important;
}

【讨论】:

    【解决方案3】:

    为了充分理解为什么在html标签中添加.cdk-global-scrollblock,我想对MatDialog Scroll Strategies做一个简短的说明:

    MatDialog 滚动策略

    MatDialog 组件有选项scrollStrategy (类型为ScrollStrategy),它决定了用于对话框的滚动策略,如:https://material.angular.io/components/dialog/api#MatDialogConfig

    要关联滚动策略,您必须将返回滚动策略的函数传递给MatDialogConfig:

    const dialogRef = this.dialog.open(DialogComponent, {
        scrollStrategy: this.overlay.scrollStrategies.block()
        // .. other options
    });
    

    默认情况下,MatDialog 将使用block 策略,如源代码code 中所示。

    其他可用策略是noop、reposition 和close - 如here 所述。


    .cdk-global-scrollblockcss类

    当使用block 策略时,此类被添加到html 标记中。 它用于阻止对话框后面内容的滚动,尤其是在移动设备 (iOS) 上 - 这就是使用 position: fixed; 的原因。但是当应用此规则时,窗口将滚动回屏幕顶部。所以需要计算当前窗口的滚动并应用到html标签上。

    源代码可以在here,在BlockScrollStrategy类中找到。我将在这里复制一些代码:

    this._previousScrollPosition = this._viewportRuler.getViewportScrollPosition();
    
    // Note: we're using the `html` node, instead of the `body`, because the `body` may
    // have the user agent margin, whereas the `html` is guaranteed not to have one.
      root.style.left = coerceCssPixelValue(-this._previousScrollPosition.left);
      root.style.top = coerceCssPixelValue(-this._previousScrollPosition.top);
      root.classList.add('cdk-global-scrollblock');
    

    解决方案

    1.真正的问题在于你的 CSS。

    为了确定this._previousScrollPosition 的值不正确,请仔细检查添加到html 和body 标记的css 规则。

    我能够使用以下 css 复制您的问题:

    html, body {
      min-height: 100%;
      height: 100%;
    }
    
    body {
     overflow: auto;
    }
    

    此处的示例:https://stackblitz.com/edit/angular-xvucu4。

    这可以通过删除body { overflow: auto; }来解决。

    此处的示例:https://stackblitz.com/edit/angular-xvucu4-jek3zb。

    如果你真的需要html和body占据100%的视口,即使内容更小,使用如下css:

    html {
        height: 100%;
    }
    
    body {
       min-height: 100%:
    }
    

    或

    2。设置noop滚动策略:

    import {Overlay} from '@angular/cdk/overlay';
    
    constructor {
      public overlay: Overlay
    }
    
    const dialogRef = this.dialog.open(DialogComponent, {
        scrollStrategy: this.overlay.scrollStrategies.noop() 
        // .. other options
    });
    

    即使打开了对话框,页面内容也会滚动。

    例如:https://stackblitz.com/edit/angular-xvucu4-t4uwwb.

    【讨论】:

    • 我没有在我的身体中使用溢出自动仍然m面临这个问题stackblitz.com/edit/angular-xvucu4?file=styles.css它只是你的堆栈闪电,我只是没有使用溢出属性但它仍然一样
    • 你能把你的例子放到stackblitz上看看源代码吗?
    • 我同意这个解决方案。在这里,我们应该使用对话框设置而不用 ::ng-deep 更改 css。
    • 我移除了我身体的最小高度,它的工作!
    【解决方案4】:

    我遇到了同样的问题。 以下解决方案对我有用,

    .cdk-global-scrollblock{
        position: static !important;
        width: initial !important;
        overflow-y: inherit !important;
      }
    

    将它放入你的全局css或对话框组件loacl css中(你必须在本地的情况下启用视图封装)

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。在我的 css 运行时看起来像这样。

      https://www.resimag.com/p1/ff8da3c59ae.jpeg

      enter image description here

      为此,我的解决方案是; 我在我的组件中导入 ViewEncapsulation 对象。您可以了解它的使用方式和作用。 https://dzone.com/articles/what-is-viewencapsulation-in-angular https://angular.io/api/core/ViewEncapsulation

      在css中我写了这段代码之后; "位置:初始;宽度:初始;溢出:隐藏;"

      "位置:初始;宽度:初始;"把它最先收到的值转回来。

      这是我的 dialog.component.css;

      .cdk-global-scrollblock {
        position: initial;
        width: initial;
        overflow: hidden;
      }
      

      这是我的 dialog.component.ts;

      import {Component, OnInit,Inject, ViewEncapsulation} from '@angular/core';
      import {  MatDialogRef,  MAT_DIALOG_DATA } from '@angular/material';
      
      @Component({
        selector: 'app-dialog',
        encapsulation: ViewEncapsulation.None,
        templateUrl: 'dialog.component.html',
        styleUrls: ['dialog.component.css']
      })
      export class DialogComponent implements OnInit {
      
        constructor(public dialogRef: MatDialogRef < DialogComponent > ,
          @Inject(MAT_DIALOG_DATA) public data: any) {}
      
        onNoClick(): void {
          this.dialogRef.close();
        }
      
        ngOnInit() {}
      
      }
      

      【讨论】:

        【解决方案6】:

        请参阅API Docs 以获取MatDialogConfig。您可以将hasBackdrop 设置为false。

            const dialogRef = this.dialog.open(DialogComponent, {
                width: '250px',
                data: { name: 'Juan Manuel', animal: 'Perro' },
                hasBackdrop: false
            });
        

        【讨论】:

          【解决方案7】:

          只需添加此覆盖 css 或 scss 类即可修复背景不可见问题。

          .cdk-global-scrollblock{
              position: initial;
          }
          

          【讨论】:

            【解决方案8】:

            要去除空白的白色页面背景,您可以使用任何一种 css :

            .cdk-global-scrollblock{
                height: auto
              }
            
            or
            
            .cdk-global-scrollblock{
                position: static !important;
                width: initial !important;
                overflow-y: inherit !important;
              }
            

            【讨论】:

            • 第一个建议对我来说效果很好。谢谢。
            猜你喜欢
            • 1970-01-01
            • 2019-07-31
            • 2020-02-22
            • 2018-05-26
            • 2019-02-07
            • 2018-01-14
            • 1970-01-01
            • 1970-01-01
            • 2020-12-30
            相关资源
            最近更新 更多