繁体   English   中英

Angular 自定义样式到 mat-dialog

[英]Angular custom style to mat-dialog

我正在尝试在 Angular 中自定义默认的“mat-dialog” 5. 我想要实现的是在对话框的上部有一个工具栏,它应该覆盖整个宽度。 但是,mat-dialog-container 有一个固定的 24px 填充,我无法覆盖它。 我尝试为 h1 和 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'
    });
}

您可以在 matDialogConfig 对象中传递自定义 panelClass( 此处为文档

所以

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

在您的自定义 panelClass 中,您可以覆盖填充:

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

但是您的 .custom-modalbox 应该是全局范围的(未在组件样式中定义)

这肯定会奏效:

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

您应该在组件上使用 panelClass,同时在 css 上使用 ::ng-deep。

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

在 CSS 中:

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

我只是改变这个,它完美地工作:

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

这里有一个工作示例: https ://stackblitz.com/edit/custom-dialog?file=src/styles.css

当您的样式是全局范围时,panelClass 可以完美地工作,否则它不会因为样式不可用。

在您的样式之前添加 ng-deep 以全局访问它!

::ng-deep {

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

}

解决方案的最佳方法是仅在一个地方更改代码。 这可以使用以下代码来完成:

::ng-deep.mat-dialog-container {
overflow: visible;
}

这有助于您仅在一个地方更改代码,而不是在所有地方更改。 这完美地工作。 除了相应的 CSS 文件之外,无需在任何地方声明任何其他内容。

它在 Angular 13 中对我有用

在 style.css 中

::ng-deep #dialogTrasparent{
  padding: 0px !important;
  box-shadow: none !important;
  background: transparent !important;

}

和组件.ts

const loader = this.dialog.open(DialogLoader, 
{id: 'dialogTrasparent'});

在全局文件中定义 CSS,我们可以使用 Mat Dialog API 添加 css

例子

   constructor(private dialogRef: MatDialogRef<MetricCreateComponent>) { }

   ngOnInit(): void {
        this.dialogRef.addPanelClass('custom-dialog-container-metric-configure');
    }


    onRemoveClick(): void {
        this.dialogRef.removePanelClass('custom-dialog-container-metric-configure');
    }

您需要构建自己的自定义 class 并在对话框属性 panelClass 中进行设置。

openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      data: this.data,
      panelClass: 'my-custom-container'
    });
}

在您的 styles.css/styles.scss 中,您写下您的自定义规则。 然后你说你想设置 mat-dialog-title 的样式,为了做到这一点,我使用了浏览器检查器并搜索了一个 class 名称作为目标(以查看实际的 class 名称 angular 将其赋予了 html 元素)。 我发现该名称为“mat-mdc-dialog-title”,并在我的规则中使用了它。

.my-custom-container {
    background-color: aqua;
}
/* Increasing css specificity by duplicating class */
/* Increasing the specificity is important to overwrite angular
   own rules on the component, without it your rules do not win 
   against angular material dialog's rules.
*/
.my-custom-container .mat-mdc-dialog-title.mat-mdc-dialog-title {
    color: blue;
}

您的对话框的 html 应如下所示:

<section class="my-custom-container">
  <h1 mat-dialog-title>Delete file</h1>
  <div mat-dialog-content>
    Would you like to delete cat.jpeg?
  </div>
  <div mat-dialog-actions>
    <button mat-button mat-button color="accent" mat-dialog-close>No</button>
    <button mat-button mat-raised-button color="primary" mat-dialog-close>Yes</button>
  </div>
</section>

不幸的是,我们无法在mat-dialog配置中设置所有需要的样式。 MatDialogConfig允许您在配置中仅设置宽度、高度、最大/最小宽度、最大/最小高度和自定义类,以由它们对某些特定选项进行操作。 但您也可以在styles.scss文件中为模式设置全局样式。 *.ts

let dialogRef = this.matDialog.open(
   SomeEntryComponent, 
   <MatDialogConfig>modalConfig // <- there you can set width/height
);
    dialogRef.afterClosed().subscribe((result: any) => { /* do stuff */ });

全局样式.scss

.cdk-overlay-pane mat-dialog-container.mat-dialog-container { // <- in this way all other styles
  margin: 20px 5px;
  padding: 30px;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM