移除 mat-form-field 输入框的下划线效果

huangapple go评论79阅读模式
英文:

Remove the underline effect from mat-form-field input

问题

我有一个使用Angular Material 14的input,当我点击输入时,默认会出现下划线。如何移除这个下划线?

<div class="filter-search">
  <mat-form-field appearance="legacy" class="filter-search-item">
    <input type="search" matInput />
  </mat-form-field>
  <mat-icon matPrefix class="my-icon">search</mat-icon>
</div>

我尝试使用appearance="none",但会出现错误Type '&quot;none&quot;' is not assignable to type 'MatFormFieldAppearance'.

此外,我还尝试使用CSS的::ng-deep,但也不起作用。查看浏览器的检查器,似乎应该可以这样工作,因为它看起来就是这样的,但实际上并不起作用。

.filter-search-item .mat-form-field-appearance-legacy .mat-form-field-underline {
  height: 0px !important;
  display: none !important;
}
英文:

I have an input using Angular Material 14 and I get an underline by default when I click on the input. How can I remove this underline?

  &lt;div class=&quot;filter-search&quot;&gt;
     &lt;mat-form-field appearance=&quot;legacy&quot; class=&quot;filter-search-item&quot;&gt;
        &lt;input type=&quot;search&quot; matInput /&gt;
      &lt;/mat-form-field&gt;
      &lt;mat-icon matPrefix class=&quot;my-icon&quot;&gt;search&lt;/mat-icon&gt;
  &lt;/div&gt;

I have tried to use appearance="none" but it gives error Type &#39;&quot;none&quot;&#39; is not assignable to type &#39;MatFormFieldAppearance&#39;.

Also using css I have used ::ng-deep but it doesn't work either. And looking in the browser's inspector this way it should work, since it is how it appears, but it doesn't work either.

.filter-search-item .mat-form-field-appearance-legacy .mat-form-field-underline {
  height: 0px !important;
  display: none !important;
}

答案1

得分: 0

问题

这是因为类型MatFormFieldAppearance的唯一两种可能的外观样式是filloutline

移除 mat-form-field 输入框的下划线效果

如果你写下以下代码:

<mat-form-field appearance="none">
  <mat-label>Input</mat-label>
  <input matInput />
</mat-form-field>

你将会得到以下错误:

error TS2322: Type '"none"' is not assignable to type 'MatFormFieldAppearance'.

为什么会出现这个错误?如果你查看tsconfig.json,你会发现strictTemplates默认设置为true

/* 要了解有关此文件的更多信息,请参阅:https://angular.io/config/tsconfig。 */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",
    "module": "es2020",
    "lib": ["es2020", "dom"],
    "noUnusedParameters": false,
    "noUnusedLocals": false,
    "strictNullChecks": true,
    "noImplicitReturns": true,
    "strictFunctionTypes": true,
    "noImplicitOverride": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictBindCallApply": true,
    "esModuleInterop": true
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true /* <-- 这是导致错误的原因 */
  }
}

解决方法

将以下CSS添加到你的组件CSS中:

form-field-overview-example.css

::ng-deep .mat-form-field-underline {
  display: none;
}

查看实时演示

注意:上面的代码将影响此组件内所有的mat-form-field元素。如果你只想针对此组件内的特定mat-form-field元素进行操作,你需要添加一个自定义类。

form-field-overview-example.html

<mat-form-field appearance="fill" class="custom-class">
  <mat-label>Input</mat-label>
  <input matInput />
</mat-form-field>

form-field-overview-example.css

::ng-deep .custom-class .mat-form-field-underline {
  display: none;
}
英文:

Problem

This is expected because the only two possible appearance styles for the type MatFormFieldAppearance are fill and outline.

移除 mat-form-field 输入框的下划线效果

If you write the following code:

&lt;mat-form-field appearance=&quot;none&quot;&gt;
  &lt;mat-label&gt;Input&lt;/mat-label&gt;
  &lt;input matInput /&gt;
&lt;/mat-form-field&gt;

You'll get the following error:

error TS2322: Type &#39;&quot;none&quot;&#39; is not assignable to type &#39;MatFormFieldAppearance&#39;.

Why do you get this error? If you look at tsconfig.json, you'll see that strictTemplates is set to true by default.

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  &quot;compileOnSave&quot;: false,
  &quot;compilerOptions&quot;: {
    &quot;baseUrl&quot;: &quot;./&quot;,
    &quot;outDir&quot;: &quot;./dist/out-tsc&quot;,
    &quot;sourceMap&quot;: true,
    &quot;declaration&quot;: false,
    &quot;downlevelIteration&quot;: true,
    &quot;experimentalDecorators&quot;: true,
    &quot;moduleResolution&quot;: &quot;node&quot;,
    &quot;importHelpers&quot;: true,
    &quot;target&quot;: &quot;es2017&quot;,
    &quot;module&quot;: &quot;es2020&quot;,
    &quot;lib&quot;: [&quot;es2020&quot;, &quot;dom&quot;],
    &quot;noUnusedParameters&quot;: false,
    &quot;noUnusedLocals&quot;: false,
    &quot;strictNullChecks&quot;: true,
    &quot;noImplicitReturns&quot;: true,
    &quot;strictFunctionTypes&quot;: true,
    &quot;noImplicitOverride&quot;: true,
    &quot;noFallthroughCasesInSwitch&quot;: true,
    &quot;noImplicitAny&quot;: true,
    &quot;noImplicitThis&quot;: true,
    &quot;strictBindCallApply&quot;: true,
    &quot;esModuleInterop&quot;: true
  },
  &quot;angularCompilerOptions&quot;: {
    &quot;enableI18nLegacyMessageIdFormat&quot;: false,
    &quot;strictInjectionParameters&quot;: true,
    &quot;strictInputAccessModifiers&quot;: true,
    &quot;strictTemplates&quot;: true /* &lt;-- This causes the error */
  }
}

Solution

Add the following CSS to your component CSS:

form-field-overview-example.css

::ng-deep .mat-form-field-underline {
  display: none;
}

See the live demo.

Note: The code above will affect all mat-form-field elements inside this component. If you want to target only specific mat-form-field elements inside this component, you need to add a custom class.

form-field-overview-example.html

&lt;mat-form-field appearance=&quot;fill&quot; class=&quot;custom-class&quot;&gt;
  &lt;mat-label&gt;Input&lt;/mat-label&gt;
  &lt;input matInput /&gt;
&lt;/mat-form-field&gt;

form-field-overview-example.css

::ng-deep .custom-class .mat-form-field-underline {
  display: none;
}

huangapple
  • 本文由 发表于 2023年7月17日 19:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定