【问题标题】:Change Color of Material Inputs on Error错误时更改材料输入的颜色
【发布时间】:2017-06-26 06:54:43
【问题描述】:

当 md-input 脏且无效时,我想将其更改为红色(与 Image 中的 Country Dropdown 相同)。我浏览了很多文章,但没有发现任何帮助..

【问题讨论】:

    标签: angular angular-material2


    【解决方案1】:

    错误颜色设置为主题中的警告颜色。

    https://material.angular.io/guide/theming

    @import '~@angular/material/theming';
    // Plus imports for other components in your app.
    
    // Include the common styles for Angular Material. We include this here so that you only
    // have to load a single css file for Angular Material in your app.
    // Be sure that you only ever include this mixin once!
    @include mat-core();
    
    // Define the palettes for your theme using the Material Design palettes available in palette.scss
    // (imported above). For each palette, you can optionally specify a default, lighter, and darker
    // hue. Available color palettes: https://material.io/design/color/
    $candy-app-primary: mat-palette($mat-indigo);
    $candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
    
    // The warn palette is optional (defaults to red).
    $candy-app-warn:    mat-palette($mat-red);
    
    // Create the theme object (a Sass map containing all of the palettes).
    $candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
    
    // Include theme styles for core and each component used in your app.
    // Alternatively, you can import and @include the theme mixins for each component
    // that you are using.
    @include angular-material-theme($candy-app-theme);
    

    【讨论】:

      【解决方案2】:

      可以通过 css 解决,但需要做好封装方面的准备,以避免像 ::ng-deep 这样的弃用变体。这里给出一个解决方案:

      https://stackoverflow.com/a/46042645/2692283

      以下是您案例的简短描述:

      1. 在你的组件中设置encapsulation: ViewEncapsulation.None

        import { ViewEncapsulation } from '@angular/core';
        
        @Component({
            ...
            encapsulation: ViewEncapsulation.None
        })
        

        这里给出更详细的解释:

        ::ng-deep going to be deprecated - Any alternatives?

      2. 现在只需使用 css 将其变为红色即可:

        /* Sets the underline to red if dirty and invalid */
        .mat-form-field.ng-dirty.ng-invalid .mat-form-field-underline {
          background-color: red;
        }
        /* Sets the label to red if dirty and invalid */
        .mat-form-field.ng-dirty.ng-invalid .mat-form-field-label {
          color: red;
        }
        

      它应该适用于 Angular 4 和 5。

      【讨论】:

        【解决方案3】:

        你需要在css下面包含

         .mat-input-invalid .mat-input-placeholder {
            color: red;
        }
        
        .mat-input-invalid .mat-input-ripple {
            background-color: red;
        }
        

        【讨论】:

        • 现在不需要了。角度材料的新更新现在也具有该功能
        • @GhanshyamSingh 请详细说明。我在文档中找不到任何内容。
        • 查看链接https://material.angular.io/components/form-field/overview
        • 您可以在输入框正下方使用<mat-error>元素。
        • @GhanshyamSingh,有效状态呢,如果输入有效,我正在尝试将颜色更改为绿色
        猜你喜欢
        • 1970-01-01
        • 2018-05-11
        • 1970-01-01
        • 2014-10-12
        • 2021-09-14
        • 2019-07-01
        • 2020-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多