问题描述
我正在使用 angular 5 和 angular material(最新版本),我正在尝试从页面打开一个对话框.当我点击触发打开的按钮时,整个网站都被置于空白背景中,就像对话框重叠其内容并将其全部隐藏一样.
I'm using angular 5 and angular material (latest version) and I'm trying to open a dialog from a page. When I click the button that triggers the opening, the entire website is put in blank background like if the dialog were overlaping it contents and hiding it all.
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() {
}
}
这是打开对话框的方法.
And this is the method that opens the Dialog.
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 标记中.
UPDATE:I've seen that after the dialog is rendered a class is added to my html tag. .cdk-global-scrollblock I don't know why is that class added to my html tag.
.cdk-global-scrollblock {
position: fixed;
width: 100%;
overflow-y: scroll;
}
那是什么导致了我的错误.有人知道为什么我的 html 标签上有那个类吗?
That what's causing my error. Does someone knows why is that class on my html tag?
推荐答案
最简单的解决方法是将对话框滚动策略设置为新的 NoopScrollStrategy
对象:
Easiest fix would be setting the dialog scroll strategy to new NoopScrollStrategy
object:
const dialogRef = this.dialog.open(DialogComponent, {
width: '250px',
data: { name: 'Juan Manuel', animal: 'Perro' },
scrollStrategy: new NoopScrollStrategy()
});
这只需要额外的导入:
import { NoopScrollStrategy } from '@angular/cdk/overlay';
这篇关于Angular Material 对话框组件隐藏了我所有的网站组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!