简体   繁体   English

不能使用带有 ng deep 的主机来设计 Angular Material Paginator 的样式吗?

[英]Cant use host with ng deep to style Angular Material Paginator?

So I have a Angular Material Paginator, which looks like this:所以我有一个 Angular Material Paginator,它看起来像这样:

在此处输入图像描述

And this is how I use it in my html:这就是我在 html 中使用它的方式:

<div class="paginator-container">
  <mat-paginator
    #paginator
    [length]="dokumente.length"
    [pageIndex]="pageIndex"
    [pageSize]="pageSize"
    [pageSizeOptions]="pageSizeOptions"
    (page)="getPaginatorData($event)"
  >
  </mat-paginator>
</div>

and this is my css to style that mat-option:这是我的 css 来设置垫子选项的样式:

//this works, but will affect other components 
::ng-deep .mat-option {
  color: black !important;
}

I tried to use host here, but strangely it did have no effect:我尝试在这里使用主机,但奇怪的是它没有效果:

//this does not work
:host ::ng-deep .mat-option {
  color: black !important;
}

And I tried to style the child element from the mat paginator, like this:我尝试从 mat paginator 中设置子元素的样式,如下所示:

//this does not work
::ng-deep .mat-paginator .mat-option {
  color: black !important;
}

Also, when I looked in my dev console, I noticed that mat-option was not inside mat-paginator or inside my component.此外,当我查看我的开发控制台时,我注意到 mat-option 不在 mat-paginator 内或我的组件内。 Like, I can not narrow it down that it belongs to my component:就像,我无法缩小它属于我的组件的范围:

在此处输入图像描述

Why is my host not working here?为什么我的房东不在这里工作? And how can I style that mat-option exclusively, without styling other mat-options in the project?以及如何在不为项目中的其他垫子选项设置样式的情况下专门设置该垫子选项的样式?

There are no available attributes that can help identify the cdkoverlay element.没有可帮助识别 cdkoverlay 元素的可用属性。

There should be only one cdkoverlay available on the DOM at any given time, assuming this you can attach a class to the mat-select-panel-wrap element and apply your styles, this a hacky way of doing it, but it should work在任何给定时间 DOM 上应该只有一个 cdkoverlay 可用,假设您可以将 class 附加到 mat-select-panel-wrap 元素并应用您的 styles,这是一种 hacky 方法,但它应该可以工作

constructor(private renderer: Renderer2){}

paginatorClicked(){
  this.renderer.addClass(document.getElementsByClassName('mat-select-panel-wrap')[0], 'black-color')
}
<div class="paginator-container">
  <mat-paginator
    #paginator
    [length]="dokumente.length"
    [pageIndex]="pageIndex"
    [pageSize]="pageSize"
    [pageSizeOptions]="pageSizeOptions"
    (page)="getPaginatorData($event)"
    (click)="paginatorClicked()"
  >
  </mat-paginator>
</div>

in your styles file add these rules在您的 styles 文件中添加这些规则

.black-color .mat-option {
  color: black !important;
}

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

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