简体   繁体   English

Angular 自定义样式到 mat-dialog

[英]Angular custom style to mat-dialog

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.我正在尝试在 Angular 中自定义默认的“mat-dialog” 5. 我想要实现的是在对话框的上部有一个工具栏,它应该覆盖整个宽度。 However, the mat-dialog-container has a fixed padding of 24px which I could not override.但是,mat-dialog-container 有一个固定的 24px 填充,我无法覆盖它。 I tried to style both the h1 and the mat-dialog-container.我尝试为 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'
    });
}

You can pass a custom panelClass in your matDialogConfig Object ( doc here )您可以在 matDialogConfig 对象中传递自定义 panelClass( 此处为文档

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 :在您的自定义 panelClass 中,您可以覆盖填充:

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

But your .custom-modalbox should be global scoped to be applied (not defined in the component styles )但是您的 .custom-modalbox 应该是全局范围的(未在组件样式中定义)

This will definitely work:这肯定会奏效:

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

You should use panelClass on the component at the same time as ::ng-deep on the css.您应该在组件上使用 panelClass,同时在 css 上使用 ::ng-deep。

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

in css:在 CSS 中:

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

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这里有一个工作示例: https ://stackblitz.com/edit/custom-dialog?file=src/styles.css

panelClass works perfectly when your styles are global scoped otherwise it won't as styles are not available.当您的样式是全局范围时,panelClass 可以完美地工作,否则它不会因为样式不可用。

Add ng-deep before your styles to access it globally!!在您的样式之前添加 ng-deep 以全局访问它!

::ng-deep {

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

}

The best way to approach the solution is to change the code only at a single place.解决方案的最佳方法是仅在一个地方更改代码。 This can be done be using the code:这可以使用以下代码来完成:

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

This helps you to change the code only at a single place rather than changing at all the places.这有助于您仅在一个地方更改代码,而不是在所有地方更改。 This works perfectly.这完美地工作。 No need to declare anything else anywhere apart from the corresponding CSS file.除了相应的 CSS 文件之外,无需在任何地方声明任何其他内容。

it worked for me in angular 13它在 Angular 13 中对我有用

in style.css在 style.css 中

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

}

and componenet.ts和组件.ts

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

Define the CSS in the global file and we can use the Mat Dialog API to add the css在全局文件中定义 CSS,我们可以使用 Mat Dialog API 添加 css

Example例子

   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');
    }

You need to build your own custom class and set it up in the dialog property panelClass.您需要构建自己的自定义 class 并在对话框属性 panelClass 中进行设置。

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

In your styles.css/styles.scss you write down your custom rules.在您的 styles.css/styles.scss 中,您写下您的自定义规则。 Then you said you wanted to style the mat-dialog-title, in order to do that I used the browser inspector and searched for a class name to target (to see the actual class name angular gave it to the html element).然后你说你想设置 mat-dialog-title 的样式,为了做到这一点,我使用了浏览器检查器并搜索了一个 class 名称作为目标(以查看实际的 class 名称 angular 将其赋予了 html 元素)。 I found that name to be 'mat-mdc-dialog-title' and I used it in my rules.我发现该名称为“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;
}

Your dialog's html should look something like this:您的对话框的 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>

Unfortunately, we can't set all desired styles in the mat-dialog config.不幸的是,我们无法在mat-dialog配置中设置所有需要的样式。 MatDialogConfig allows you to set only width, height, max/min-width, max/min-height, and custom classes in the config to operate by them for some specific options. MatDialogConfig允许您在配置中仅设置宽度、高度、最大/最小宽度、最大/最小高度和自定义类,以由它们对某些特定选项进行操作。 But also you can set up global styles for modal in styles.scss file.但您也可以在styles.scss文件中为模式设置全局样式。 *.ts *.ts

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

global styles.scss全局样式.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