【发布时间】:2020-12-22 08:59:14
【问题描述】:
我在我的 html 中添加了一个 mat-hint 元素。 我只想在用户关注相应的表单字段时显示 mat-hint 并隐藏 focusout 上的提示。 如何为所有表单域实现此场景。
<mat-hint align="end">Max 50 characters</mat-hint>
【问题讨论】:
我在我的 html 中添加了一个 mat-hint 元素。 我只想在用户关注相应的表单字段时显示 mat-hint 并隐藏 focusout 上的提示。 如何为所有表单域实现此场景。
<mat-hint align="end">Max 50 characters</mat-hint>
【问题讨论】:
您可以将(focus) 和(focusout) 与称为focusState 的引用一起使用
<input name="date" type="text" (focus)="focusState = true" (focusout)="focusState = false">
<mat-hint align="end" *ngIf="focusState">Max 50 characters</mat-hint>
处理多个项目
<div *ngFor="let item of array">
<input #item name="date" type="text" (focus)="item.alt = true" (focusout)="item.alt = false">
<mat-hint align="end" *ngIf="item.alt">Max 50 characters</mat-hint>
</div>
【讨论】:
*ngFor 进行迭代。对于这种情况,您可以为每个项目创建一个单独的变量并独立执行相同的操作。更新答案和此处的链接,这将有助于实现这一点。 stackblitz.com/edit/angular-ckqmvs
我只通过 css 解决了这个问题。不集中时隐藏。
.mat-form-field:not(.mat-focused) .mat-hint {
display: none;
}
【讨论】: