在Angular Material中,如果你的对话框的panelClass属性不会更新,你可以尝试以下解决方法:
import { ChangeDetectorRef } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
constructor(private dialog: MatDialog, private cdr: ChangeDetectorRef) {}
openDialog() {
const dialogRef = this.dialog.open(MyDialogComponent);
dialogRef.componentInstance.panelClass = 'my-custom-class';
dialogRef.componentInstance.someData = 'some data';
dialogRef.afterOpened().subscribe(() => {
this.cdr.detectChanges(); // 手动检测变更
});
}
import { AfterViewInit } from '@angular/core';
export class MyDialogComponent implements AfterViewInit {
// ...
ngAfterViewInit() {
this.panelClass = 'my-custom-class';
this.someData = 'some data';
}
}
import { MatDialog } from '@angular/material/dialog';
constructor(private dialog: MatDialog) {}
openDialog() {
const dialogRef = this.dialog.open(MyDialogComponent);
setTimeout(() => {
dialogRef.componentInstance.panelClass = 'my-custom-class';
dialogRef.componentInstance.someData = 'some data';
}, 0);
}
通过使用这些解决方法,你应该能够更新Angular Material对话框的panelClass属性。