[英]angular material dialog is not responsive
我正在使用下面的Angular 材质对话框是我为打开对话框而编写的代码,其中我正在传递配置:
组件.ts
let dialogBoxSettings = {
height: '300px',
width: '500px',
disableClose: true,
hasBackdrop: true,
data: mission.config
};
const dialogRef = this.dialog.open(
DialogBoxComponent,
dialogBoxSettings
);
我已将宽度和高度传递给对话框,但是当我调整浏览器窗口的大小时,对话框会移动到窗口的左侧。
对话框.component.ts:
import { Component, Inject, OnInit,HostListener } from "@angular/core";
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from "@angular/material";
import { Subscription } from "rxjs/Subscription";
declare let jQuery: any;
@Component({
selector: "dialog-box",
templateUrl: "./dialog-box.component.html",
styleUrls: ["./dialog-box.component.css"]
})
export class DialogBoxWidgetComponent implements OnInit {
title: string = "Title";
heading: string = "Heading";
type: string = "userInput;";
buttons: any;
public dialogConfig: any;
subscription: Subscription;
constructor(
public dialogRef: MatDialogRef<DialogBoxWidgetComponent>,
@Inject(MAT_DIALOG_DATA) private data: any
) {}
closeDialog() {
jQuery(".close").trigger("click");
}
ngOnInit() {
this.dialogConfig = this.data;
this.title = this.dialogConfig.title;
this.heading = this.dialogConfig.heading;
this.type = this.dialogConfig.type;
this.buttons = this.dialogConfig.buttons;
jQuery(".cdk-overlay-container").css("z-index", 99999);
jQuery(".mat-dialog-container").css({
"border-radius": "7px"
});
}
}
对话框.component.html
<h1 matDialogTitle>{{title}}</h1>
<div (click)="closeDialog()" style="position: relative;">
<button class="close" mat-dialog-close></button>
</div>
<div mat-dialog-content>{{heading}}</div>
<div mat-dialog-actions *ngIf="type == 'userInput'">
<button *ngFor="let item of buttons" [ngClass]="item.type=='button'?'mat-dialog-btn':'mat-dilalog-btn-link'" mat-button matDialogClose="{{item.value}}">{{item.name}}</button>
</div>
我的实施有什么问题? 如果我不给对话框宽度和高度,那么当我调整窗口大小时它会调整得很好。
为了实现响应性,我们可以在 MatDialogconfig 中使用 panelClass
以全局样式添加以下内容
.dialog-responsive {
width: 40%;
}
@media only screen and (max-width: 760px) {
.dialog-responsive {
width: 100%;
}
}
并将此类包含在您要打开对话框的配置中
let config: MatDialogConfig = {
panelClass: "dialog-responsive"
}
let dialogRef = this.dialog.open(InviteStudentComponent, config);
您可以尝试添加margin: 0 auto;
到您的对话框设置:
let dialogBoxSettings = {
height: '300px',
width: '500px',
margin: '0 auto',
disableClose: true,
hasBackdrop: true,
data: mission.config
};
我在我的情况下使用了低效用类(顺风),这是我找到的解决方案:
let dialogRef = this.dialog.open(LiveEventModalComponent, {
panelClass: ['md:w-3/5', 'w-full'],
maxHeight: '85vh',
data: {},
});
只需在全局样式文件中应用 CSS 媒体查询:
mat-dialog-container{
max-width: 50%;
margin: auto;
}
@media (max-width: 500px){
mat-dialog-container{
max-width: 80vw;
}
}
我使用 paneClass 并在 styles.scss 中设置我的自定义样式。 请记住,您必须在 mat-dialog 中设置 maxWidth 属性,因为当您不设置 maxWidth 时,它使用默认值,而您的 maxWidth 为 80vw。
const dialogRef = this.dialog.open(ErrorDialogComponent, {
maxWidth: '500px',
data: { errorText: message },
panelClass: 'full-with-dialog',
});
在styles.scss 中:
.full-with-dialog {
position: relative;
overflow: auto;
width: 500px;
@media (max-width: 568px) {
width: 440px;
}
@media (max-width: 468px) {
width: 340px;
}
@media (max-width: 368px) {
width: 300px;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.