您可以使用以下代码示例解决Angular材料对话框的对齐问题:
在HTML文件中:
Title
Content
在TS文件中:
import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {
@ViewChild('dialogTemplate') dialogTemplate: TemplateRef;
constructor(
public dialog: MatDialog
) { }
ngOnInit() {
}
openDialog() {
const dialogRef = this.dialog.open(this.dialogTemplate, {
hasBackdrop: true,
disableClose: true,
width: '500px',
panelClass: 'custom-dialog-container'
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
});
}
}
在CSS文件中:
.custom-dialog-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
min-height: 400px;
}
在这个示例中,我们在组件中定义了一个TemplateRef,它包含了对话框的标题和内容。然后,在组件的构造函数中注入了MatDialog服务,以便在“openDialog”方法中打开通过该TemplateRef定义的对话框。在对话框打开时,我们使用open方法向对话框传递了“panelClass”选项,并将其设置为“custom-dialog-container”这个CSS类名。最后,我们在CSS文件中定义了这个类名的样式,以确保对话框垂直和水平居中。