在Angular/Material中设置mat-form-field输入样式

phy*_*boy 10 html javascript css angular-material2 angular

我有一个带有输入的mat-form-field,我想添加一个自定义样式,但是我在官方Angular Material网站上找不到任何关于此的文档.

我最终的目标是:

  • 选择输入框后更改下划线颜色
  • 删除浮动标签(如果可能 - 我知道这是一个功能,但现在已弃用).

我不是最适合Angular的人,但是如果需要改变JS中的东西,那么我总能给它最好的机会.

我只需要一点指导.

现行代码:

<form class="search-form">
  <mat-form-field class="example-full-width">
    <input class="toolbar-search" type="text" matInput>
    <mat-placeholder>Search</mat-placeholder>
    <mat-icon matSuffix style="font-size: 1.2em">search</mat-icon>
  </mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)

目前的CSS:

// Change text colour when inputting text
.toolbar-search, mat-placeholder {
  color: white;
}

// Changes the underline and placeholder colour before input is selected
/deep/ .mat-input-underline {
    background-color: white;
}
Run Code Online (Sandbox Code Playgroud)

A. *_*rel 13

要使用scss更改材料输入的样式:

标准:

::ng-deep .mat-form-field {
    .mat-input-element {
        color: slategray;
    }
    .mat-form-field-label {
        color: slategray;
    }
    .mat-form-field-underline {
        background-color: slategray;
    }
    .mat-form-field-ripple {
        background-color: slategray;
    }
    .mat-form-field-required-marker {
        color: slategray;
    }
}
Run Code Online (Sandbox Code Playgroud)

重点关注:(选中时)

::ng-deep .mat-form-field.mat-focused {
    .mat-form-field-label {
        color: #ff884d;
    }
    .mat-form-field-ripple {
        background-color: #ff884d;
    }
    .mat-form-field-required-marker {
        color: #ff884d;
    }
    .mat-input-element {
        color: #ff884d;
    }
}
Run Code Online (Sandbox Code Playgroud)

无效:

::ng-deep .mat-form-field.mat-form-field-invalid {
    .mat-input-element {
        color: #ff33cc;
    }
    .mat-form-field-label {
        color: #ff33cc;
        .mat-form-field-required-marker {
            color: #ff33cc;
        }
    }
    .mat-form-field-ripple {
        background-color: #ff33cc;
    }
}
Run Code Online (Sandbox Code Playgroud)

演示

你也可以使用ViewEncapsulation.None,以避免::ng-deep弃用

import { ViewEncapsulation } from '@angular/core';

@Component({
    ...
    encapsulation: ViewEncapsulation.None
})
Run Code Online (Sandbox Code Playgroud)


Ian*_*ici 6

根据我的理解,这两个功能似乎都在MatFormField中.

  1. 不推荐使用floatPlaceholder,因为它现在是使用输入应用的[floatLabel]FloatLabelType('never', 'always', 'auto')
  2. 您可以使用输入更改下划线的颜色[color],但是您只能从主题('primary', 'accent', 'warn')中选择颜色.有关如何设置主题的更多信息,请访问他们的网站,


<form class="search-form">
  <mat-form-field class="example-full-width"
                  floatLabel="never" color="primary">
    <input class="toolbar-search" type="text" matInput placeholder="search">
    <mat-icon matSuffix style="font-size: 1.2em">search</mat-icon>
  </mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)


Dav*_*nck 5

您可以使用下面使用的css选择器:

/deep/ .mat-input-underline {
   background-color: white;
}
Run Code Online (Sandbox Code Playgroud)

/深/组合子被提名为在角弃用,所以最好不用它做的事。不幸的是,来自Angular Material的.mat-input-underline是高度指定的,这使得不使用/ deep /很难覆盖。

我发现最好的方法是使用ID,与默认的Angular Material样式相比,它可以为您提供更高的特异性。

 <form id="search-form" [formGroup]="form" (ngSubmit)="submit()">
    <mat-form-field>
      <mat-placeholder class="placeholder">Search</mat-placeholder>
      <input type="text" class="toolbar-search" matInput formControlName="search">
      <mat-icon matSuffix>search</mat-icon>
    </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

然后,您的“搜索表单” ID可用于定位输入。在不破坏视图封装的情况下,您不能在component.scss中定位mat-form-field-underline。通过将其添加到您的global.scss,可以更轻松地在全局级别执行此操作

global.scss:

#search-form {
  .mat-form-field-underline {
    background-color: $accent;
  }
  .mat-form-field-ripple {
    background-color: $accent;
  }
  .placeholder {
    display: none;
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望Angular Material团队在将来撤回其特殊性,因为目前尚无简便方法可以覆盖其默认设置。