angular - 将鼠标悬停在垫标签上时不会触发 mouseenter 和 mouseleave

标签 angular angular-material

悬停在 mat-filed-set 标签 (mat-label) 上时无法捕获 (mouseenter) 和 (mouseleave) 事件。

当用户将鼠标悬停在某些表单字段标签上时,我需要在屏幕底部显示一些上下文帮助信息。

 <mat-form-field floatLabel="always">
         <!-- this does not work -->
         <mat-label class="mat-h1"  
            (mouseenter)="mouseHovering($event)" 
            (mouseleave)="mouseLeave()">Background information
          </mat-label>

         <!-- this works -->
         <mat-icon class="md-18" mat-button 
            (mouseenter)="mouseHovering($event") 
            (mouseleave)="mouseLeave()" color="primary">info
         </mat-icon>
          :
          :
          some control
          :
 </mat-form-field>
// These methods are never triggered (when hovering the mat-label)
  mouseHovering($event) {
    :
    //(logic to display a bottom sheet)
  }

  mouseLeave() {
    :
    //(logic to display a bottom sheet)
  }

能够以与不使用 mat-label 相同的方式捕获事件

最佳答案

原因是因为 Angular 在 mat-label 组件上有 pointer-events: none 样式。他们这样做是为了当标签不处于 float 状态时,用户在输入悬停时看到正确的光标鼠标图标。您可以使用 css 轻松启用指针事件:

mat-label.has-events {
  pointer-events: auto;
}

<mat-label class="has-events">

或内联:

<mat-label style="pointer-events:auto;">

这应该没问题,因为您处于“始终” float 设置,但是如果您的输入不处于 float 状态,这将导致您的输入在悬停时显示错误的光标。您也可以通过在元素上设置正确的光标来解决此缺点,例如在 css 中设置 cursor: text

这样的 css 可以修复它,以便点事件仅在 float 状态下触发,但也保留默认光标:

mat-form-field.mat-form-field-should-float mat-label.has-events {
  pointer-events: auto;
  cursor: default;
}

此处演示了此行为和 css 选项:https://stackblitz.com/edit/angular-klmnau?file=app/form-field-label-example.html

关于angular - 将鼠标悬停在垫标签上时不会触发 mouseenter 和 mouseleave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57315736/

相关文章:

javascript - 页脚中的水平居中链接与具体化

angular - 编译入口点@angular/material/select 失败

javascript - Angular md-virtual-repeat 不更新更改的数据

angular - 带有 Angular Material 延迟加载问题的 InjectionToken mat-select-scroll-strategy 错误

javascript - setDefaultTheme 方法的 Angular Material 主题问题?

angular - 如果指令太高,请替换输入数字上的数字

javascript - 当 JWT token 在 Angular 7 中过期时如何显示警报或 toastr 消息

Angular + Material - 如何使用表单组处理多个复选框

javascript - Angular 2 如何使用 Observable 分配 JSON 响应数组并在 typescript 接口(interface)中映射并使用 *ngFor 在 html 中检索?

Angular 5 snackbar 通知