【问题标题】:How to change color of radio button in angular material 2如何更改角材料2中单选按钮的颜色
【发布时间】:2017-03-01 10:36:54
【问题描述】:

我正在努力解决下一个问题。我正在尝试以正确的方式更改角材料 2 radio button 的颜色,而不覆盖 css。默认情况下,它使用材质主题的强调色,但我想使用主色。

其他组件具有颜色属性,如checkboxbutton 等...

提前致谢。

【问题讨论】:

标签: angular angular-material2


【解决方案1】:

如果其他人偶然发现此问题,您现在可以在 MatRadioButton 组件上使用 color 属性。这是我更正的 GitHub 问题的链接:

【讨论】:

  • 颜色属性不起作用。您所能做的就是使用预定义的颜色,例如此处的示例:stackblitz.com/edit/angular-material2-issue-ethbcx?file=app/… 添加属性 color="#54b76d" 不起作用。从长远来看,这不是正确的答案。
  • 原来的问题要求使用模板的原色,这就是为什么我按照我的方式给出答案。如果 OP 询问如何使用非模板颜色,那么我会以不同的方式回答。
【解决方案2】:

ng-deep 已被弃用。将此代码复制到您的 styles.scss 文件中。它会起作用的。

.mat-radio-button.mat-accent .mat-radio-ripple .mat-ripple-element {
opacity: 0 !important; /*click effect color change*/
background-color: #422873 !important;  }

.mat-radio-button.mat-accent .mat-radio-inner-circle {
background-color: #422873 !important; /*inner circle color change*/  }

.mat-radio-button.mat-accent.mat-radio-checked .mat-radio-outer-circle {
border-color: #422873 !important; /*outer ring color change*/  }

【讨论】:

    【解决方案3】:

    单选按钮组件没有输入更改颜色。 您需要在 .scss 文件中添加以下行:

    /deep/ .mat-radio-outer-circle {
    .mat-radio-checked & {
          border-color: #3f51b5;
        }
    }
    
    /deep/ .mat-radio-inner-circle {
        background-color: #3f51b5;
    }
    
    /deep/ .mat-radio-ripple .mat-ripple-element {
        background-color: rgba(#3f51b5, 0.26);
    }
    

    【讨论】:

    • /deep/ 已被弃用,仅适用于视图模拟视图封装。
    【解决方案4】:

    这对我有用

    .mat-radio-button.mat-accent.mat-radio-checked .mat-radio-outer-circle , .mat-radio-outer-circle {
      border-color: blue !important;
    }
    
    .mat-radio-button.mat-accent .mat-radio-inner-circle{
      background-color: blue !important;
    }
    

    【讨论】:

    • angular 7 它的::ng-deep b4 选择器,不需要!important
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2020-10-29
    • 2018-05-11
    • 2021-10-30
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多