css “有角材料”对话框面板类不更新

a5g8bdjr  于 2023-03-20  发布在  其他
关注(0)|答案(4)|浏览(140)

我正在使用“Angular 材质”对话框,并尝试将背景设置为自定义颜色。
这个问题已经被问过很多次了,我试着应用答案,但是似乎没有用。具体来说,对话框容器的panelClass似乎没有更新。下面是打开对话框的组件、_theming.scss文件和HTML元素

import { Component, OnInit} from '@angular/core';
import { AuthService } from 'src/app/AuthenticationPackage/core/auth.service'
import { MatDialog, MatDialogConfig } from '@angular/material';
import { FactiondialogComponent } from './factiondialog/factiondialog.component';


@Component({
  selector: 'app-factions2',
  templateUrl: './factions2.component.html',
  styleUrls: ['./factions2.component.scss']
})
export class Factions2Component implements OnInit {

  constructor( public authService: AuthService,
            public dialog: MatDialog ) { }

  ngOnInit(){  }


  openDialog(faction): void{
    const dialogConfig = new MatDialogConfig()

    dialogConfig.disableClose = true;
    dialogConfig.autoFocus = true;
    dialogConfig.data = {faction};
    dialogConfig.panelClass = ".faction-dialog";

    const dialogRef = this.dialog.open(FactiondialogComponent, dialogConfig)

        dialogRef.afterClosed().subscribe(result => {
            console.log("dialog closed");
        });
}
}

_theming.scss部分:

@mixin mat-dialog-theme($theme) {
  $background: map-get($theme, background);
  $foreground: map-get($theme, foreground);

  .faction-dialog{
    background-color:rgb(28, 31, 32)
}

  .mat-dialog-container {
    @include _mat-theme-elevation(24, $theme);
    background: mat-color($background, dialog);
    color: mat-color($foreground, text);
  }
}

@mixin mat-dialog-typography($config) {
  .mat-dialog-title {
    @include mat-typography-level-to-styles($config, title);
  }
}

这是生成的标记,但它不包括我的自定义类。

<mat-dialog-container aria-modal="true" class="mat-dialog-container ng-tns-c12-2 ng-trigger ng-trigger-dialogContainer ng-star-inserted" tabindex="-1" id="mat-dialog-0" role="dialog" style="transform: none;"><!--bindings={
  "ng-reflect-portal": ""
}--><app-factiondialog _nghost-lda-c13="" class="ng-star-inserted"><div _ngcontent-lda-c13="" class="dialogCard"><h2 _ngcontent-lda-c13="" class="mat-dialog-title">The Harpers</h2><mat-dialog-content _ngcontent-lda-c13="" class="mat-typography mat-dialog-content"><p _ngcontent-lda-c13="">
SOME CONTENT THAT DOESNT MATTER TO THE EXAMPLE
            </p></mat-dialog-content><mat-dialog-actions _ngcontent-lda-c13="" align="end" class="mat-dialog-actions"><button _ngcontent-lda-c13="" mat-button="" mat-dialog-close="" class="mat-button mat-button-base" ng-reflect-dialog-result="" type="button"><span class="mat-button-wrapper">Close</span><div class="mat-button-ripple mat-ripple" matripple="" ng-reflect-centered="false" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLButtonElement]"></div><div class="mat-button-focus-overlay"></div></button></mat-dialog-actions></div></app-factiondialog></mat-dialog-container>

我相信上半部分应该说:

<mat-dialog-container aria-modal="true" class="mat-dialog-container faction-dialog ng-tns-c12-2 ng-trigger ng-trigger-dialogContainer ng-star-inserted" tabindex="-1" id="mat-dialog-0" role="dialog" style="transform: none;">

但我不确定,因为我还没有得到这个工作。我已经按照文档:
https://material.angular.io/components/dialog/api#MatDialogConfig
我不确定是否有什么需要添加到我的应用程序模块或其他地方。
根据Khan先生的要求:
faction2.component.ts:

openDialog(faction): void{


    const dialogRef = this.dialog.open(FactiondialogComponent, {panelClass: 'faction-dialog'})

        dialogRef.afterClosed().subscribe(result => {
            console.log("dialog closed");
        });
}

模态打开屏幕:

HTML检查元素

<mat-dialog-container aria-modal="true" class="mat-dialog-container ng-tns-c12-4 ng-trigger ng-trigger-dialogContainer ng-star-inserted" tabindex="-1" id="mat-dialog-2" role="dialog" style="transform: none;"><!--bindings={
  "ng-reflect-portal": ""
}--><app-factiondialog _nghost-yis-c13="" class="ng-star-inserted"><div _ngcontent-yis-c13="" class="dialogCard"><h2 _ngcontent-yis-c13="" class="mat-dialog-title"></h2><mat-dialog-content _ngcontent-yis-c13="" class="mat-typography mat-dialog-content"><p _ngcontent-yis-c13=""></p></mat-dialog-content><mat-dialog-actions _ngcontent-yis-c13="" align="end" class="mat-dialog-actions"><button _ngcontent-yis-c13="" mat-button="" mat-dialog-close="" class="mat-button mat-button-base" ng-reflect-dialog-result=""><span class="mat-button-wrapper">Close</span><div class="mat-button-ripple mat-ripple" matripple=""></div><div class="mat-button-focus-overlay"></div></button></mat-dialog-actions></div></app-factiondialog></mat-dialog-container>
70gysomp

70gysomp1#

如果要添加自己的自定义类来设置材质模态的样式,请首先将自定义类传递给模态中的panelClass键,方法如下:

this.dialogRef = this.dialog.open(AddCustomComponent,{
    panelClass: 'custom-dialog-container', //======> pass your class name
});
this.dialogRef.afterClosed();

一旦这样做了,你所要做的就是使用你的类来设计你的模态,其他的模型就不会受到影响。例如,你可以用这种方法来移除填充和边距。

/*Material Dialog Custom Css*/ 
.custom-dialog-container .mat-dialog-container{
    padding: 0;
}

.custom-dialog-container .mat-dialog-container .mat-dialog-content{
    margin: 0;
}
/*---------------------------*/
mcdcgff0

mcdcgff02#

panelClass被添加到对话框容器的父容器中,所以空格就是我所缺少的:

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

还可以使用ng-deep或将样式放在根样式表中,而不是组件x1c 0d1x中

jtw3ybtb

jtw3ybtb3#

我想添加唯一一个对我有效的解决方案。我尝试了这个线程中的所有方法和许多其他方法,但没有任何效果。最终我意识到对话框不使用属性background或background-color,而是-mdc-dialog-container-color。将颜色直接应用于此属性对我有效。
styles.scss(全局样式表)

.confirmationPopUp .mat-mdc-dialog-container {
  --mdc-dialog-container-color: #015945;
  color: mat.get-color-from-palette($my-primary, 50);
}

组件的.ts

openPopUp(
    enterAnimationDuration: string,
    exitAnimationDuration: string
  ): void {
    this.dialog.open(PopUp, {
      width: '250px',
      enterAnimationDuration,
      exitAnimationDuration,
      panelClass: 'confirmationPopUp',
    });
  }

类输入动画持续时间:字符串,退出动画持续时间:string只是设置对话框打开/关闭的动画。
希望这对其他人有帮助!上述解决方案可能在某个时候起作用了,但似乎不再起作用了:)

pkbketx9

pkbketx94#

上面的回答很好,但我只是想扩展一下,MatDialogConfig也可以通过一些配置对象传递。
例如,我为我的场景传入了自定义类SingleViewDialog,其中my-custom-maximizepadding覆盖被添加到mat-dialog.scss覆盖文件中:

export interface SingleViewDialog {
  examUID: string;
  examData: TrendOctExam;
  layoutMode: Layout;  
}

public launchSingleView(examUID: string, examData: TrendOctExam) {
    const config: MatDialogConfig<SingleViewDialog> = {};
    config.hasBackdrop = true;
    config.disableClose = false;
    config.panelClass = 'my-custom-maximize';
    const mode: Layout = 'volume2d';
    config.data = { examUID, examData, layoutMode: mode};
    
    const dialogRef = this.matDialog.open(OCTThreeDSingleComponent, config );   //*** INJECT HERE ***
    
    dialogRef.afterClosed().subscribe(_ => this.viewStateService.updateLayoutMode('glaucoma'));
  }

相关问题