简体   繁体   English

材料 ui 复选框 css 覆盖 angular 中的组件

[英]Material ui checkbox css is overriding across the components in angular

I'm trying to override material UI checkbox css in my Home Component and it is working fine.我正在尝试在我的主页组件中覆盖材质 UI 复选框 css,它工作正常。 But there are side effects like css of checkbox in form component is overriding.但是有副作用,例如表单组件中的复选框的 css 被覆盖。 Can anyone suggest a solution for this.任何人都可以为此提出解决方案。

HTML used HTML 使用

 <mat-checkbox formControlName="home">
  Home
</mat-checkbox>

css used for over riding by default it is grey color css 用于超车,默认为灰色

::ng-deep .mat-checkbox .mat-checkbox-frame {
border-color: blue !important;
}

Form component mat checkbox - for this component also it is overriding border color to blue without writing any css表单组件垫复选框 - 对于此组件,它也将边框颜色覆盖为蓝色而不写入任何 css

<mat-checkbox formControlName="form">
  Form
</mat-checkbox>

I believe this is happening due to usage of ::ng-deep我相信这是由于使用::ng-deep而发生的

I even tried ViewEncapsulation also in the Home component.我什至在 Home 组件中也尝试过ViewEncapsulation It is still overriding css in Form component and other css in both the components.它仍然覆盖表单组件中的 css 和两个组件中的其他 css。

Any help regarding this would be appreciated.对此的任何帮助将不胜感激。

To apply the style only inside a specific child component, add the :host selector to the following code in the CSS of that component:要仅在特定子组件内应用样式,请将:host选择器添加到该组件的 CSS 中的以下代码中:

:host ::ng-deep .mat-checkbox .mat-checkbox-frame {
  border-color: blue !important;
}

This will scope this rule to all checkboxes which are in this current component and all its children and will work pretty well in case of routed components.这将 scope 这条规则适用于当前组件及其所有子组件中的所有复选框,并且在路由组件的情况下会很好地工作。

But if you want to stick this css rule only for home page template then you can use:但是,如果您只想将此 css 规则用于主页模板,则可以使用:

home.component.css home.component.css

.mat-checkbox ::ng-deep  .mat-checkbox-frame {
  border-color: blue !important;
}

Angular will replace it with: Angular 将其替换为:

.mat-checkbox[_ngcontent-rvb-c0] .mat-checkbox-frame {
    border-color: blue !important;
}

where _ngcontent-rvb-c0 is unique identifier of current compoent其中_ngcontent-rvb-c0是当前组件的唯一标识符

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

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