[英]Angular/Material mat-form-field input - Floating label issues
Is there any way in which I can stop the placeholder from floating as a label for the following snippet of code?有什么方法可以阻止占位符作为以下代码片段的标签浮动?
<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>
I have looked at the official documentation for angular/material, but it seems this feature is now deprecated?我已经查看了 angular/material 的官方文档,但现在似乎不推荐使用此功能?
assuming you are using latest stable version of material 2,假设您使用的是材料 2 的最新稳定版本,
you can use floatLabel="never" to force label to not to float.您可以使用floatLabel="never"强制标签不浮动。
here is live working demo这是现场工作演示
this is clear in documentation https://material.angular.io/components/form-field/api这在文档https://material.angular.io/components/form-field/api 中很清楚
<form class="search-form">
<mat-form-field class="example-full-width" appearance="standard">
<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>
Please set the appearance of mat-form-field to standard and the placeholder will stop behaving like label.请将 mat-form-field 的外观设置为标准,占位符将不再像标签一样表现。
Explanation : By default the mat-label in mat-form-field floats and the appearance of mat-form-field is "legacy".说明:默认情况下,mat-form-field 中的 mat-label 是浮动的,并且 mat-form-field 的外观是“legacy”。 That means if a mat-label is not present with the form field then placeholder will start behaving like the label and it floats upwards.
这意味着如果表单字段中不存在 mat-label,那么占位符将开始表现得像标签一样,它会向上浮动。 So to stop this you should manually update the appearance to "standard", so it stops behaving like label and stays as a placeholder.
因此,要停止此操作,您应该手动将外观更新为“标准”,这样它就不再表现得像标签一样并保持为占位符。
Praveen Soni's answer above doesn't work for non-legacy versions, as per this:上面的 Praveen Soni 的回答不适用于非遗留版本,如下所示:
Note: only the legacy appearance supports the never option.
注意:只有旧外观支持从不选项。 never was originally added as a way to make the floating label emulate the behavior of a standard input placeholder.
never 最初添加的目的是使浮动标签模拟标准输入占位符的行为。 However the form field now supports both floating labels and placeholders.
但是,表单字段现在支持浮动标签和占位符。 Therefore in the non-legacy appearances the never option has been disabled in favor of just using the placeholder.
因此,在非传统外观中,已禁用 never 选项以支持仅使用占位符。
That means you should simply use placeholder instead of the label.这意味着您应该简单地使用占位符而不是标签。
1.Use floatLabel="never" in mat-form-field 1.在mat-form-field中使用floatLabel="never"
<mat-form-field floatLabel="never">
</mat-form-field>
<div class="custom_autosuggestion">
<mat-form-field class="example-full-width" floatLabel="never">
<input class="custom_color_title"
aria-label="Number" matInput [formControl]="myColorControl" type="text" name="" [matAutocomplete]="auto">
<mat-placeholder class="custom_placeholder">{{templateParse.Search_Colors_by_Name_or_Code}}</mat-placeholder>
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredColorOptions | async" [value]="option">
<!-- {{option}} -->
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.