【发布时间】:2017-06-26 06:54:43
【问题描述】:
【问题讨论】:
【问题讨论】:
错误颜色设置为主题中的警告颜色。
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);
【讨论】:
可以通过 css 解决,但需要做好封装方面的准备,以避免像 ::ng-deep 这样的弃用变体。这里给出一个解决方案:
https://stackoverflow.com/a/46042645/2692283
以下是您案例的简短描述:
在你的组件中设置encapsulation: ViewEncapsulation.None:
import { ViewEncapsulation } from '@angular/core';
@Component({
...
encapsulation: ViewEncapsulation.None
})
这里给出更详细的解释:
现在只需使用 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。
【讨论】:
你需要在css下面包含
.mat-input-invalid .mat-input-placeholder {
color: red;
}
.mat-input-invalid .mat-input-ripple {
background-color: red;
}
【讨论】:
https://material.angular.io/components/form-field/overview
<mat-error>元素。