英文:
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 '"none"' 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
?
<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>
I have tried to use appearance="none" but it gives error Type '"none"' is not assignable to type 'MatFormFieldAppearance'.
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
的唯一两种可能的外观样式是fill
和outline
。
如果你写下以下代码:
<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
.
If you write the following code:
<mat-form-field appearance="none">
<mat-label>Input</mat-label>
<input matInput />
</mat-form-field>
You'll get the following error:
error TS2322: Type '"none"' is not assignable to type 'MatFormFieldAppearance'.
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. */
{
"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 /* <-- 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
<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论