Angular custom style to mat-dialog

末鹿安然 提交于 2020-06-24 11:10:45

问题


I am trying to customize the default "mat-dialog" in Angular 5. What I want to achieve is having a toolbar in the upper part of the dialog, which should cover the whole width. However, the mat-dialog-container has a fixed padding of 24px which I could not override. I tried to style both the h1 and the mat-dialog-container.

@Component({
selector: 'error-dialog',
template: 
` <h1 mat-dialog-title> ERRORE </h1>             
    <div mat-dialog-content>
        {{data.error}}
    </div>
    <div mat-dialog-actions>
        <button mat-button (click)="onClick()">Ok</button> 
    </div>`,
styles: [
'h1 { background: #E60000; color: white; }',
// 'myDialogStyle .mat-dialog-container { padding: 1px !important;}'
]})

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

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

openErrorDialog(errore: string): void{
    let dialogRef = this.dialog.open(ErrorDialog, {
        width: '80%',
        data: { error: errore }
        //panelClass: 'myDialogStyle'
    });
}

回答1:


You can pass a custom panelClass in your matDialogConfig Object (doc here)

so

openErrorDialog(errore: string): void{
let dialogRef = this.dialog.open(ErrorDialog, {
    width: '80%',
    data: { error: errore }
    panelClass: 'custom-modalbox'
});

}

And in your custom panelClass you can override the padding :

.custom-modalbox {
    mat-dialog-container {
        padding: 0;
    }
}

But your .custom-modalbox should be global scoped to be applied (not defined in the component styles )




回答2:


I just change this, it works perfectly:

.custom-modalbox >  mat-dialog-container {
        padding: 0px;
}

Here there is a working example: https://stackblitz.com/edit/custom-dialog?file=src/styles.css




回答3:


This will definitely work.

::ng-deep.mat-dialog-container {
padding: 0px !important;

}




回答4:


panelClass works perfectly when your styles are global scoped otherwise it won't as styles are not available.

Add ng-deep before your styles to access it globally!!

::ng-deep {

 .custom-dialog > mat-dialog-container {
        padding: 0px;
    }

}


来源:https://stackoverflow.com/questions/48688614/angular-custom-style-to-mat-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!